๐Ÿ“˜ TypeScript - Interface

zooyahoยท2022๋…„ 9์›” 10์ผ
0
post-thumbnail

์ธํ„ฐํŽ˜์ด์Šค

  • ๊ตฌ์ฒด์  ๊ฐ’์ด ์•„๋‹Œ ๊ตฌ์กฐ์™€ ํ˜•ํƒœ ์ง€์ •
  • ๊ฐ์ฒด์˜ ํƒ€์ž…์„ ํ™•์ธํ•˜๋Š”๋ฐ ์‚ฌ์šฉ
  • ์ปดํŒŒ์ผ ์‹œ interface์˜ ํ”์ ์€ ์‚ฌ๋ผ์ง.

๐Ÿ‘พ

interface Greetable {
  name: string;
  age: number;
  greet(phrase: string): void;
}

/*
type Greetable = {
  name: string;
  age: number;
  greet(phrase: string): void;
}
*/

let user1: Greetable;

user1 = {
  name: "Max",
  age: 25,
  greet(phrase: string) {
    console.log(phrase + " " + this.name);
  },
};
user1.greet("Hi there - I am");

โญ๏ธ ์‚ฌ์šฉ์ž ์ •์˜ type๊ณผ์˜ ์ฐจ์ด์ 

  • ์ธํ„ฐํŽ˜์ด์Šค๋Š” ๊ฐ์ฒด์˜ ๊ตฌ์กฐ๋ฅผ ์„ค๋ช…ํ•˜๊ธฐ์œ„ํ•ด ์‚ฌ์šฉ
  • ์ž‘์—… ์ค‘ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š” ํด๋ž˜์Šค๊ฐ€ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ดํ–‰ํ•˜๊ณ  ์ค€์ˆ˜ํ•ด์•ผํ•˜๋Š” ์•ฝ์†์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

โ— class์— interface ์ง€์ •

  • 1๊ฐœ ์ด์ƒ์˜ interface๋ฅผ implementsํ•  ์ˆ˜ ์žˆ๋‹ค.
interface Greetable {
  name: string;
  age: number;
  greet(phrase: string): void;
}

class Person implements Greetable {
  name: string;
  age = 30;
  constructor(n: string) {
    this.name = n;
  }
  greet(phrase: string) {
    console.log(phrase + " " + this.name);
  }
}
let user1: Greetable;
// let user1: Person; ๋„ ๊ฐ€๋Šฅ

user1 = new Peson("Max");
user1.greet("Hi there - I am");

โ— ์ฝ๊ธฐ ์ „์šฉ ์†์„ฑ - readonly

  • ์ฝ๊ธฐ ์ „์šฉ์œผ๋กœ ์„ค์ •ํ•˜์—ฌ ๊ฐ์ฒด๊ฐ€ ์ดˆ๊ธฐํ™”๋˜๋ฉด ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋„๋ก ํ•œ๋‹ค.
  • type์—์„œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ interface์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค.
interface Greetable {
  readonly name: string;
  greet(phrase: string): void;
}

class Person implements Greetable {
  name: string;
  constructor(n: string) {
    this.name = n;
  }
  greet(phrase: string) {
    console.log(phrase + " " + this.name);
  }
}
let user1: Greetable;
user1 = new Peson("Max");
user1.name = 'MArk'; // error

โ— ์ธํ„ฐํŽ˜์ด์Šค ํ™•์žฅ - extends

  • interface์— extends๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ™•์žฅํ•œ๋‹ค.
  • class์—์„œ extends๋Š” 2๊ฐœ ์ด์ƒ์˜ ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์„ ์ˆ˜ ์—†๋‹ค.
interface Named {
  readonly name: string;
}
interface Greetable extends Named{
  greet(phrase: string): void;
}

class Person implements Greetable {
  name: string;
  constructor(n: string) {
    this.name = n;
  }
  greet(phrase: string) {
    console.log(phrase + " " + this.name);
  }
}
let user1: Greetable;
user1 = new Peson("Max");

โ— ํ•จ์ˆ˜ ํƒ€์ž…์œผ๋กœ์„œ์˜ interface

// type AddFn = (a: number, b: number) => number;
interface AddFn {
  (a: number, b: number): number;
}

let addFn: AddFn;
addFn = (n1: number, n2: number) => n1 + n2;

โ— ์„ ํƒ์  ๋งค๊ฐœ๋ณ€์ˆ˜์™€ ์†์„ฑ - ?

  • ์„ ํƒ์  ๋ฉ”์„œ๋“œ : FnName?(): void....
interface Named {
  readonly name: string;
  outputName?: string; // ์„ ํƒ์  ์†์„ฑ
  greet?(phrase: string): void; // ์„ ํƒ์  ๋ฉ”์„œ๋“œ
}

class Person implements Named {
  name?: string;

  constructor(n?: string) { // ์„ ํƒ์  ๋งค๊ฐœ๋ณ€์ˆ˜ ์„ค์ •
  // ๊ธฐ๋ณธ๊ฐ’์ด ์„ค์ •๋˜์ง€ ์•Š์•„์„œ ์ •์˜๋˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์— ?๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ๊ธฐ๋ณธ๊ฐ’์„ ํ• ๋‹นํ•œ๋‹ค.
  // (n: string = '') -> ๊ธฐ๋ณธ๊ฐ’ ํ• ๋‹น
    if(n) {
      this.name = n;
    }
  }
}
let user1: Greetable;
user1 = new Peson(); // error๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์Œ
profile
์ฆ๊ฒ๊ฒŒ ๊ฐœ๋ฐœํ•˜์ž ์ฅฌ์•ผํ˜ธ๐Ÿ‘ป

0๊ฐœ์˜ ๋Œ“๊ธ€