패스트캠퍼스 데브캠프

김민태의 데브캠프 2기 - interface, type

vitamin3000 2024. 11. 11. 12:02

 

이번 시간에는 typescript의 interface와 type의 선언 방법과 그 특징에 대해 알아보고자 한다.

 

 

먼저 interface이다 .

 

interface Animal {
    name: string;
}

interface Bear extends Animal{
    honey: boolean;
}

const Bear1: Bear = {
    name: 'heoney beadr',
    honey: true
}

 

Animal로 동일한 이름의 interface를 두개 선언하고, 이것을 extends로 상속받아 객체로 생성, 접근하면 따로 만들었음에도 접근 할 수 있다.

 

이번엔 type이다.

 

type Animal = {
    name: string;
}

type Bear = Animal & {
    honey: boolean;
}

const bear1 = {
    name: 'honey bear',
    honey: true
}

 

위에서 interface의 경우엔 extends로 상속받았는데, type는 & 연산자를 통해 상속 받아 접근할 수 있다.