비교
공통점
- 객체를 정의하고 타입을 할당할 수 있다.
- class의 implements가 될 수 있다.
- 상속이 가능하다.
interface AInterface extends BInterface {}
type AType = Btype & { a: number}
Only Interface
- interface는 interface 간의 merge가 가능하다
interface A {
name: string;
}
interface A {
age: number;
}
const obj: A = {
}
Only Type Alias
- Union type을 구현할 수 있다.
type Direction = 'left' | 'right' | 'up' | 'down';
무엇을 사용해야할까?
Interface
- 어떤 특정한 규격을 정의해야 한다면, 그리고 이 규격을 통해서 객체나 클래스를 구현해야 한다면
interface
를 사용한다.
interface
는 "이 interface를 구현해놓은 클래스가 있을까?" 라는 생각을 하게 한다.
- 어떤 것을 구현할 목적으로 선언한다.
Types (type alias)
- 어떤 데이터를 담을 때, 데이터의 타입을 결정해야 한다면,
type alias
를 사용한다.