CS

[TypeScript] Interface vs Type

크라00 2024. 7. 9. 22:11

< Interface >

1. extends 키워드를 통해서 확장할 수 있다.

2. 선언적확장 ( Declaration Merging) 가능

   - 같은 이름의 interface 를 선언시 자동 확장 )

   - 타입 객체 확장성에 유리하다.

3. 원시 자료형에는 사용할 수 없다. ( string, number )

4. computed value 사용이 불가능하다.

 

< Type >

1. & 기호를 이용해서 확장할 수 있다.

2. 선언적 확장이 불가능하다.

3. 원시 자료형, 튜플, 유니언 타입을 선언할 수 있다.

 

type Name = string; // primitive

type Age = number;

type Person = [string, number, boolean]; // tuple

type NumberString = string | number; // union

 

4. computed value 사용이 가능하다.