ν‹°μŠ€ν† λ¦¬ λ·°

TypeScript

πŸ“’ Interfaces

yunieyunie 2022. 12. 16. 11:21

πŸ“œ Interfaces

Interfacesλž€ μ–΄λ–€ νƒ€μž…μ„ λ§Œλ“œλŠ” 방식인데 내뢀적인 것과 상관없이 μ™ΈλΆ€μ μœΌλ‘œ λ“œλŸ¬λ‚˜λŠ” 객체의 μ‚¬μš©λ°©μ‹μ΄ 적힌 νƒ€μž…μ΄λ‹€.

function hello(person: { name: string; age: number; }): void { 
      console.log(`μ•ˆλ…•ν•˜μ„Έμš”! ${person.name} μž…λ‹ˆλ‹€.`);
  }
  
  const p: { name: string; age: number; } = { 
      name: 'Mark',
      age: 35 
  };
  
  hello(p); // μ•ˆλ…•ν•˜μ„Έμš”! Mark μž…λ‹ˆλ‹€.



  //Interfaces μ‚¬μš©ν•˜λ©΄

  interface Person {
      name: string; 
      age: number; 
  }
  
  function hello(person: Person): void {
      console.log(`μ•ˆλ…•ν•˜μ„Έμš”! ${person.name} μž…λ‹ˆλ‹€.`); 
  }
  
  const p: Person = { 
      name: 'Mark', 
      age: 35
  };
  
  hello(p); // μ•ˆλ…•ν•˜μ„Έμš”! Mark μž…λ‹ˆλ‹€.

❓ optional property

상황에 따라 μžˆμ„ μˆ˜λ„ 없을 μˆ˜λ„ μžˆλŠ” optional property
크게 두 κ°€μ§€ 방법이 μžˆλŠ”λ° 첫번째 방법은 λ‹€μŒκ³Ό 같이 ?λ₯Ό λ„£λŠ” 것이닀.

interface Person { 
      name: string; 
      age?: number;
  }

  function hello(person: Person): void {
      console.log(`μ•ˆλ…•ν•˜μ„Έμš”! ${person.name} μž…λ‹ˆλ‹€.`); 
  }

  const p1: Person = { 
      name: 'Mark', 
      age: 35
  };

  const p2: Person = { 
      name: 'Anna' 
  };

  hello(p1); // μ•ˆλ…•ν•˜μ„Έμš”! Mark μž…λ‹ˆλ‹€. 
  hello(p2); // μ•ˆλ…•ν•˜μ„Έμš”! Anna μž…λ‹ˆλ‹€.

?λ„£λŠ” 방법이 μ•„λ‹Œ 두 번째 방법은 Indexable types이닀.
μ–΄λ–€ μ΄λ¦„μ˜ ν”„λ‘œνΌν‹°κ°€ 와도 상관없닀.

interface Person {
      name: string;
      age?: number; 
      [index: string]: any; 
  }

  function hello(person: Person): void {
      console.log(`μ•ˆλ…•ν•˜μ„Έμš”! ${person.name} μž…λ‹ˆλ‹€.`); 
  }

  const p1: Person = { 
      name: 'Mark', 
      age: 35,
  };

  const p2: Person = { 
      name: 'Anna', 
      systers: ['Joy', 'Hani'] 
  };

  const p3: Person = { 
      name: 'Bokdaengi', 
      father: p1,
      mother: p2 
  };

  hello(p1); // μ•ˆλ…•ν•˜μ„Έμš”! Mark μž…λ‹ˆλ‹€.
  hello(p2); // μ•ˆλ…•ν•˜μ„Έμš”! Anna μž…λ‹ˆλ‹€.
  hello(p3); // μ•ˆλ…•ν•˜μ„Έμš”! Bokdaengi μž…λ‹ˆλ‹€.

🧑 function in interface

interface Person { 
      name: string; 
      age: number; 
      hello(): void; 
  }

  const p1: Person = { 
      name: 'Mark', 
      age: 35,
      hello: function (): void {
          console.log(this);
          console.log(`μ•ˆλ…•ν•˜μ„Έμš”! ${this.name} μž…λ‹ˆλ‹€.`);
      } 
  };

  const p2: Person = { 
      name: 'Mark',
      age: 35,
      hello(): void {
          console.log(this);
          console.log(`μ•ˆλ…•ν•˜μ„Έμš”! ${this.name} μž…λ‹ˆλ‹€.`);
      } 
  };

  const p3: Person = { 
      name: 'Mark', 
      age: 35,
      hello: (): void => {
          console.log(this);
          console.log(`μ•ˆλ…•ν•˜μ„Έμš”! ${this.name} μž…λ‹ˆλ‹€.`);//this μ—λŸ¬
      } 
  };

  p1.hello(); // μ•ˆλ…•ν•˜μ„Έμš”! Mark μž…λ‹ˆλ‹€. 
  p2.hello(); // μ•ˆλ…•ν•˜μ„Έμš”! Mark μž…λ‹ˆλ‹€. 
  p3.hello(); // μ•ˆλ…•ν•˜μ„Έμš”! μž…λ‹ˆλ‹€.

πŸ’› class implements interface

interfaceλ₯Ό μ΄μš©ν•΄ classλ₯Ό λ§Œλ“€ 수 μžˆλ‹€.

interface IPerson { 
      name: string; 
      age?: number; 
      hello(): void; 
  };

  class Person implements IPerson { 
      name: string;

      constructor(name: string) { //name은 μ΄ˆκΈ°κ°’μ„ μ§€μ •ν•΄μ€˜μ•Ό 함
          this.name = name;
      }
      hello(): void {
          console.log(`μ•ˆλ…•ν•˜μ„Έμš”! ${this.name} μž…λ‹ˆλ‹€.`);
      } 
  };

  const person = new Person('Mark'); 
  person.hello(); // μ•ˆλ…•ν•˜μ„Έμš”! Mark μž…λ‹ˆλ‹€.

πŸ’š interface extends interface

interface 끼리 상속(extends)ν•΄λ³΄μž.

interface Person { 
      name: string; 
      age?: number; 
  };

  interface Korean extends Person { 
      city: string;
  };

  const k: Korean = { 
      name: 'μ΄μ›…μž¬', 
      city: 'μ„œμšΈ'
  };

πŸ’™ function interface

ν•¨μˆ˜μ˜ νƒ€μž… μ²΄ν¬λŠ” ν• λ‹Ήν•  λ•Œκ°€ μ•„λ‹ˆλΌ μ‚¬μš©ν•  λ•Œ ν•œλ‹€λŠ” 점을 λͺ…μ‹¬ν•˜μž.

interface HelloPerson {
      // (name: string, age: number): void;
      (name: string, age?: number): void; 
  };

  let helloPerson: HelloPerson = function (name: string) { 
      console.log(`μ•ˆλ…•ν•˜μ„Έμš”! ${name} μž…λ‹ˆλ‹€.`);
  };

  helloPerson('Mark'); // μ•ˆλ…•ν•˜μ„Έμš”! Mark μž…λ‹ˆλ‹€.

πŸ‘€ Readonly Interface Properties

μΈν„°νŽ˜μ΄μŠ€ 속성을 readonly (읽기 μ „μš©)으둜 μ§€μ •ν•΄λ³΄μž.
ν”„λ‘œνΌν‹°κ°€ ν•œ 번 λ§Œλ“€μ–΄μ§€κ³  λ°”λ€Œμ§€ μ•ŠλŠ” 값이라면 readonly λ₯Ό λΆ™μ—¬μ•Ό ν•œλ‹€.

interface Person8 {
      name: string; 
      age?: number;
      readonly gender: string;
  };

  const person: Person8 = { 
      name: "Mark",
      gender: "male",
  };

  person.gender = "female" // μ—λŸ¬

πŸ†š type alias vs interface

type alias 와 interface λ₯Ό λΉ„κ΅ν•΄λ³΄μž


function

// type alias
  type EatType = (food: string) => void; 

  // interface
  interface IEat {
    (food: string): void; 
  }

array

// type alias
  type PersonList = string[]; 

  // interface
  interface IPersonList { 
    [index: number]: string; 
  }

intersection

interface ErrorHandling { 
    success: boolean;
    error?: { message: string }; 
  }

  interface ArtistsData {
    artists: { name: string }[]; 
  }

  // type alias
  type ArtistsResponseType = ArtistsData & ErrorHandling; 

  // interface
  interface IArtistsResponse extends ArtistsData, ErrorHandling {} //닀쀑 상속

  let art: ArtistsResponseType;
  let iar: IArtistsResponse;

union types

interface Bird { 
    fly(): void;
    layEggs(): void; 
  }

  interface Fish { 
    swim(): void; 
    layEggs(): void; 
  }

  // type alias
  type PetType = Bird | Fish;

  // interfaceλŠ” λΆˆκ°€
  interface IPet extends PetType {} // error TS2312: An interface can only extend an 
  object type or intersection of object types with statically known members.

  class Pet implements PetType {} // error TS2422: A class can only implement an 
  object type or intersection of object types with statically known members.

Declaration Merging

// type aliasλŠ” λΆˆκ°€
  type MergingType = { 
    a: string;
  };

  type MergingType = { //이름이 λ˜‘κ°™μ•„μ„œ μ—λŸ¬
    b: string;
  };

  // interface 
  interface MergingInterface { 
    a: string;
  }
  interface MergingInterface { 
    b: string;
  }
  let mi: MergingInterface; 
  mi. // a와 bκ°€ λ¨Έμ§•λ˜μ–΄ λ‘˜ λ‹€ λ‚˜μ˜¨λ‹€.

'TypeScript' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

πŸ“— Classes  (0) 2022.12.19
πŸ“’ TypeScript Compiler  (0) 2022.12.15
πŸ“˜ Type System  (0) 2022.12.13
λŒ“κΈ€