Skip to main content

타입을 집합으로 이해하기

  • never: 공집합
const x: never = 12;
// Type 'number' is not assignable to type 'never'.(2322)
  • unknown: 전체집합
  • 리터럴타입: 원소가 1개인 집합
  • 값이 T에 할당 가능: 값이 T의 원소
  • T1이 T2에 할당 가능: T1이 T2의 부분집합
  • T1이 T2를 상속: T1이 T2의 부분집합
  • T1 | T2: T1과 T2의 합집합
type A = "A";
type B = "B";
type Twelve = 12;

type AB = "A" | "B";
type AB12 = "A" | "B" | 12;

const a: AB = "A";
const c: AB = "C";
// Type '"C"' is not assignable to type 'AB'.(2322)
  • T1 & T2: T1과 T2의 교집합
interface Person {
name: string;
}

interface Lifespan {
birth: Date;
death?: Date;
}

type PersonSpan = Person & Lifespan;

const ps: PersonSpan = {
name: "Alan Turing",
birth: new Date("1912/06/23"),
death: new Date("1954/0607"),
};