타입스크립트에서 타입을 선언하는 방법은 2가지가 있다.
type으로 선언하는 방법과 interface로 선언하는 방법이다.
내가 느끼기에 type alias와 interface의 기능은 대부분 비슷한 것 같다.
type Type1 = {
num: number
}
const data1: Type1 = { num: 1 }
interface Interface1 {
num: number
}
const data2: Interface1 = { num: 2 }
type Fruit = 'apple' | 'banana'
type Vegetable = 'carrot' | 'tomato' | 'onion'
type Food = Fruit | Vegetable
const food: Food = 'apple'
type Student = [name: string, age: number, major: string]
const student: Student = [ 'AA', 20, 'Computer Engineering']
공식문서에서는 기본으로 interface를 사용하고 type의 특성이 필요한 경우에만 type을 사용하라고 적혀있다.