[TypeScript]Interface

jr_necki·2022년 8월 23일
0

TypeScript

목록 보기
7/7
post-thumbnail

✔ Interface

인터페이스는 객체의 모양을 특정해주는 역할을 한다.

Team 이라는 콜을 만들때, 그냥 string이 아닌 구체적인 단어로 설정하면, (또는 Health를 구체적인 숫자로 설정하면)

type Team = "read" | "blue" | "yellow"
type Health = 1 | 5 | 10


interface Player{
    nickname:string,
    team:Team,
    health:Health
}


그 외의 것은 받을 수 없다.

타입으로 해도 상관은 없다.

type Player = {
    nickname:string,
    team:Team,
    health:Health
}

✔ interface의 상속

interface User{
    name:string
}

interface Player extends User{

}

const necki : Player = {
    name:"necki"
}

상속도 가능하다.

✔ abstract 쓰는거 보다 interface가 더 좋은듯..

인터페이스는 가볍다. 인터페이스는 컴파일하면 js로 바뀌는것이 아니라 사라진다.
여러개 implements 받을 수 도 있고~

profile
슉슉슉

0개의 댓글