📘 TypeScript

TypeScript - 타입 별칭(Alias)

zunwon 2023. 11. 20. 16:38

타입 별칭(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,
};