타입 별칭(Alias)
- 새로운 타입 조합 생성
- 각각의 타입에 이름을 부여한 후에 원하는 곳에서 재사용할 수 있다
type MyTypeName = string | number;
type MyArray = MyTypeName[]; // 별칭 사용
type UserA = {
name: string;
age: number;
};
type UserB = {
isValid: boolean;
};
type UserX = UserA & UserB;
const a: MyTypeName = "A"; // MyTypeName은 string 또는 number 할당 가능
const b: MyArray = [1, "A", "B", 2, 3]; // MyArray는 MyTypeName[]과 같으므로 배열 item으로 string 또는 number 할당 가능
const userA: UserA = {
name: "BAEK",
age: 27,
};
const userB: UserB = {
isValid: true,
};
const userX: UserX = {
name: "BAEK",
age: 27,
isValid: true,
};
'📘 TypeScript' 카테고리의 다른 글
TypeScript - 함수의 명시적 this 타입 (0) | 2023.11.21 |
---|---|
TypeScript - 인터페이스(Interface) (1) | 2023.11.20 |
TypeScript - 타입 가드(Guard) (0) | 2023.11.20 |
TypeScript - 타입, 할당 단언(Assertion) (0) | 2023.11.20 |
TypeScript - 타입 추론(Inference) (0) | 2023.11.20 |