본문 바로가기

TypeScript

[TypeScript] 인터섹션 타입 Intersection Type

인터섹션 타입은 교집합의 개념이다.

 

인터섹션 타입(Intersection Type)이란?

AND 연산자와 같이 'A이면서 B이다' 라는 의미의 타입이다.

 

예를 들어, 인턴은 학생이면서 동시에 노동자이다.

아래와 같이 인턴 타입학생 타입노동자 타입의 요구사항을 모두 만족해야 한다.

type Student = {
  name: string;
  score: number;
};

type Worker = {
  employeeId: number;
  work: () => void;
};

type Intern = Student & Worker;

 

결과적으로 인턴 타입은 아래와 같이 정의된다.

{
  name: string;
  score: number;
  employeeId: number;
  work: () => void;
}