본문 바로가기

TypeScript

interfaces 타입스크립트 인터페이스란?

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

타입을 조건선택으로 만들수도있다. 저기에 적혀있는것 외는 오류가 뜬다.

 

인터페이스란?

한가지 용도만 가지고 있다.

오브젝트의 모양을 특정해주기 위한것.

인터페이스는 오로지 오브젝트이 모양을 타입스크립트에게 설명해주기 위한것.

interface hello = string 안됨

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

인터페이스는 오직 오브젝트만을 설명한다.

인터페이스는 타입스크립트에게 오브젝트의 모양을 설명해 주기 위해 존재한다.

 

타입과 인터페이스의 비교

interfaces 예시

interface User {
    name:string
}
interface Player extends User{

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

type 예시

type User = {
    name:string
}
type Player = User & {

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