[Typescript] Interface vs. type

민병대·2023년 5월 8일
0

Differences Between Type Aliases and Interfaces

Type aliases and interfaces are very similar, and in many cases you can choose between them freely. Almost all features of an interface are available in type, the key distinction is that a type cannot be re-opened to add new properties vs an interface which is always extendable.

유형 별칭과 인터페이스는 매우 유사하며, 대부분의 경우 자유롭게 선택할 수 있습니다. 인터페이스의 거의 모든 기능을 유형으로 사용할 수 있으며, 핵심적인 차이점은 항상 확장 가능한 인터페이스와 비교하여 유형을 다시 열어 새 속성을 추가할 수 없다는 것입니다.

type은 새로운 프로퍼티에 열려있지 않고,
interface는 항상 열려있다

interface

interface Person {
  name: string;
}

interface Person {
  age: number;
}

const user: Person = {
  name: 'name',
  age: 25,
};

type

type Window = {
  title: string
}

type Window = {
  ts: number
}

// Error: Duplicate identifier 'Window'.
profile
마케터 출신 개발자

0개의 댓글