Interface

Interface

  • ํƒ€์ž…๋“ค์˜ ์ด๋ฆ„์„ ์ง“๋Š” ์—ญํ• 
  • ์ฝ”๋“œ ์•ˆ์˜ ๊ณ„์•ฝ์„ ์ •์˜
  • ํ”„๋กœ์ ํŠธ ์™ธ๋ถ€์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์ฝ”๋“œ์˜ ๊ณ„์•ฝ์„ ์ •์˜
interface Age {
  age: number;
}

function personAge(personObj: Age) {
  console.log(personObj.key);
}

let person = { name: "job", age: 20 };
personAge(person);
  • personAge๋Š” Age์˜ ํƒ€์ž…์„ ๊ฐ€์ ธ์•ผ ํ•œ๋‹ค.
  • โœจ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ธ์ž๋กœ ๋ฐ›์•„ ์‚ฌ์šฉํ•  ๋•Œ ์ธํ„ฐํŽ˜์ด์Šค์˜ ์†์„ฑ ๊ฐฏ์ˆ˜์™€ ์ธ์ž๋กœ ๋ฐ›๋Š” ๊ฐ์ฒด์˜ ์†์„ฑ ๊ฐฏ์ˆ˜๋ฅผ ์ผ์น˜์‹œํ‚ค์ง€ ์•Š์•„๋„ ๋œ๋‹ค.
  • โœจ ํ•จ์ˆ˜์— ์ „๋‹ฌ๋œ ๊ฐ์ฒด๊ฐ€ ๋‚˜์—ด๋œ ์š”๊ตฌ ์กฐ๊ฑด์„ ์ถฉ์กฑํ•˜๋ฉด, ํ—ˆ์šฉ๋œ๋‹ค.

์„ ํƒ์  ํ”„๋กœํผํ‹ฐ (Optional Properties)

  • ์„ ํƒ์  ํ”„๋กœํผํ‹ฐ๋Š” ์„ ์–ธ์—์„œ ํ”„๋กœํผํ‹ฐ ์ด๋ฆ„ ๋์— ?๋ฅผ ๋ถ™์—ฌ ํ‘œ์‹œ
  • โœจ ์ฆ‰, ์žˆ์–ด๋„ ๋˜๊ณ  ์—†์–ด๋„ ๋œ๋‹ค๋Š” ํ‘œ์‹œ
interface Student {
	name : string;
  	grade? : number;
}

let student1 = {
	name : 'bob';
};

function newStudent(student:Student){
	console.log(student.name);//bob
}
  • โœจ newStudent() ํ•จ์ˆ˜์—์„œ student ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ธ์ž์˜ ํƒ€์ž…์œผ๋กœ ์„ ์–ธํ–ˆ์ง€๋งŒ, ์ธ์ž๋กœ ๋„˜๊ธด ๊ฐ์ฒด์—๋Š” grade๊ฐ€ ์—†๋‹ค โžก๏ธ grade๋ฅผ ์„ ํƒ์  ํ”„๋กœํผํ‹ฐ๋กœ ์„ ์–ธํ–ˆ๊ธฐ ๋•Œ๋ฌธ!

์ฝ๊ธฐ์ „์šฉ ํ”„๋กœํผํ‹ฐ (Read0nly properties)

  • ํ”„๋กœํผํ‹ฐ ์ด๋ฆ„ ์•ž์— readonly๋ฅผ ๋ถ™ํžˆ๊ฒŒ ๋˜๋ฉด ๋”์ด์ƒ ์ˆ˜์ •์ด ๋ถˆ๊ฐ€๋Šฅํ•ด์ง„๋‹ค.
interface Point {
  readonly x: number;
  readonly y: number;
}

let p1: Point = { x: 10, y: 20 };
p1.x = 8; //์˜ค๋ฅ˜ - ๋ถˆ๊ฐ€๋Šฅ!

ReadonlyArray<T>

  • ๋ฐฐ์—ด ์„ ์–ธ ์‹œ, ReadonlyArray<T> ํƒ€์ž…์„ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ๊ธฐ์ „์šฉ ๋ฐฐ์—ด ์ƒ์„ฑ ๊ฐ€๋Šฅ
let arr: number[] = [1, 2, 3, 4, 5];
let readNum: ReadonlyArray<number> = arr;

readNum[0] = 10; // ์—๋Ÿฌ
readNum.push(8); // ์—๋Ÿฌ
arr = readNum; // ์—๋Ÿฌ
  • ReadonlyArray๋ฅผ ์ผ๋ฐ˜ ๋ฐฐ์—ด์— ์žฌํ• ๋‹น ๋ถˆ๊ฐ€๋Šฅ

๊ฐ์ฒด ์„ ์–ธ๊ณผ ๊ด€๋ จ๋œ ํƒ€์ž… ์ฒดํ‚น (Excess Property Checks)

interface NewZoo {
  animal?: string;
  size?: number;
}

function createZoo(zoo: NewZoo) {
  //...
}

createZoo({ animel: "lion" }); // error: Object literal may only specify known properties, but 'animel' does not exist in type 'NewZoo'. Did you mean to write 'animal'?

let korZoo = { animel: "lion" };

NewZoo ์ธํ„ฐํŽ˜์ด์Šค์—๋Š” animal ์ด๋ผ๊ณ  ์„ ์–ธ๋˜์–ด์žˆ์ง€๋งŒ createZoo()ํ•จ์ˆ˜์— ์ธ์ž๋กœ ๋„˜๊ธฐ๋Š” korZoo ๊ฐ์ฒด์—๋Š” animel์ด ์„ ์–ธ๋˜์–ด ์žˆ์–ด ์˜คํƒˆ์ž๋ฅผ ์ ๊ฒ€ํ•˜๋ผ๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒ ๐Ÿ’ฅ

๐Ÿš€ ํƒ€์ž… ์ถ”๋ก ์„ ๋ฌด์‹œํ•˜๊ณ  ์‹ถ์„ ๋•Œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ ์–ธ

  • createZoo(korZoo as NewZoo)
  • ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜ํ•˜์ง€ ์•Š์€ ์†์„ฑ๋“ค์„ ์ถ”๊ฐ€๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์„ ๋•Œ
interface NewZoo {
  animal?: string;
  size?: number;
  [propName: string]: any;
}

ํ•จ์ˆ˜ ํƒ€์ž… (Function Types)

  • ํ•จ์ˆ˜์˜ ํƒ€์ž…์„ ์ •์˜ํ•  ๋•Œ๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
interface login {
  (username: string, password: string): boolean;
}
  • ํ•จ์ˆ˜์˜ ์ธ์ž์˜ ํƒ€์ž…๊ณผ ๋ฐ˜ํ™˜ ๊ฐ’์˜ ํƒ€์ž… ์ •์˜
let loginUser: login;
loginUser = function (id: string, pw: string) {
  console.log("๋กœ๊ทธ์ธ ์„ฑ๊ณต");
  return true;
};

์ธํ„ฐํŽ˜์ด์Šค ํ™•์žฅ (Extending Interfaces)

interface Person {
  name: string;
}

interface Developer extends Person {
  skill: string;
}

let he = {} as Developer;
he.name = "jack";
he.skill = "TypeScript";
  • ์—ฌ๋Ÿฌ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ™•์žฅํ•  ์ˆ˜ ์žˆ๊ธฐ์—, ๋ชจ๋“  ์ธํ„ฐํŽ˜์ด์Šค์˜ ์กฐํ•ฉ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.
interface Hobby {
  hobby: string;
}

interface Developer extends Person, Hobby {
  isJob: boolean;
}

let he = {} as Developer;
he.hobby = fishing;
he.isJob = false;