- 더 심화한 타입 검사를 수행하는데 필요한 지식
교차 타입(Intersection)
- 여러 가지 타입을 결합하여 하나의 단일 타입 만들 수 있음
- 기존 다른 타입들을 합쳐 해당 타입의 모든 멤버를 가지는 새로운 타입을 생성함
- 결과물로 탄생한 단일 타입에는 타입 별칭(type alias)를 붙일 수 있음.
사용방법
type ProductItem = {
id: number;
name: string;
type: string;
price: number;
imageUrl: string;
quantity: number;
}
type ProductItemWithDiscount = ProductItem & { discountAmount: number } ;
유니온 타입(Union)
- 유니온 타입은 타입 A 또는 타입 B 중 하나가 될수 있는 타입을 말함
- 주로 특정 변수가 가질 수 있는 타입을 전부 나열하는 용도로 사용
사용방법
type CardItem = {
id: number;
name: string;
type: string;
imageUrl: string;
};
type PromotionEventItem = ProductItem | CardItem;