Type Alias 란?
- Type Alias 는 새로운 타입을 정의할 때 사용한다.
- 타입으로 사용할 수 있다는 점에서 Type Alias 는 Interface 와 유사하다.
Type 사용 방법
type PositionType = {
x: number;
y: number;
};
const position: PositionType = {
x: 3,
y: 4
}
Type 의확장
- Interface 와 type 을 비교해보면 확장 기능에 있어서 interface 가 조금 더 준수한 성능을 가지고 있다고 한다.
(최신 버전에서는 성능으로 인한 차이는 없다고 확인)
- type 과 interface 를 사용하는 데에는 팀 내 규칙에 따라서 달라지곤 하지만 일반적으로 type 을 사용할 때에는 단순히 값을 담아두기 위해 사용하는 경우가 많다.
- type 은 interface 처럼 선언적 확장이 불가능하다.
type ExtendType = PositionType & {
z: number
}
const newPosition: ExtendType = {
x: 3, y: 5, z: 3
}
type Score = "A" | "B" | "C" | "F";
type Job = "police" | "developer" | "teacher";
type T1 = string | number | Boolean
type EatType = (name: string) => void
const eatFood: EatType = (n) => {
console.log('eat',n)
}