TypeScript
Type Script_05 Intersection Type (인터섹션 타입)
ram_Hi
2021. 4. 16. 15:07
1. Intersection Type
' & '의 의미를 갖고 있는 타입. 타입 두가지를 함께 쓰고 싶을 때 쓰고, " 타입1 & 타입2 " 이렇게 사용한다.
함수 호출시, 타입1과 타입2에 들어있는 모든 요소들을 사용해야 컴파일 오류 나지 않는다.
type Juice = {
name: string;
gram: number;
};
type Coffee = {
source: string;
make: () => void;
};
function juicyStore(menu: Juice & Coffee) {
console.log(menu.name);
console.log(menu.gram);
console.log(menu.source);
console.log(menu.make());
}
juicyStore({
name: 'OrangeKiwi', // 만약 입력 안할 시 컴파일 오류 발생
gram: 500,
source: Mocha,
make: () => {},
});