본문 바로가기
Frontend/TypeScript

[TypeScript] 타입 VS 인터페이스

by 민두이 2023. 1. 31.
728x90
반응형
  • 참고 링크 :

https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#differences-between-type-aliases-and-interfaces

 

Documentation - Everyday Types

The language primitives.

www.typescriptlang.org

 

공통점

타입스크립트에게 오브젝트의 모양과 타입을 알려주기 위함

 

차이점

1. 타입은 새 property를 추가하기 위해 재선언이 불가하지만 interface는 가능

2. 상속 방법

 

타입 상속 

type PlayerA = {
    firstName : string,
    lastName : string
}

type PlayerAA = PlayerA & {
    age : number
}

const playerA : PlayerAA = {
    firstName : "a",
    lastName : "b",
    age : 10
}

 

인터페이스 상속

interface PlayerB {
    firstName : string,
    lastName : string
}

interface PlayerBB extends PlayerB {
    age : number
}

const playerB : PlayerBB = {
    firstName : "B",
    lastName : "bb",
    age: 11
}
728x90