개발언어/TypeScript
교차타입 (Intersection Types)
Ligion
2021. 4. 12. 13:55
Intersection Type은 여러 타입을 묶어서 합쳐준다.
(Intersection이 교차, 사거리라는 뜻인데...뭔가 애매하다)
type Student = {
name: string;
score: number;
};
type Worker = {
empolyeeId: number;
work: () => void;
};
function internWork(person: Student & Worker) {
console.log(person.name);
console.log(person.score);
console.log(person.empolyeeId);
console.log(person.work());
}
internWork({
name: 'steve',
score: 50,
empolyeeId: 80,
work: () => {},
});
internWork라는 함수에서는 Student와 Worker 타입을
&로 묶어준 person으로 새로운 타입을 생성하여 파라미터로 받는다.
함수 내부에서는 요소들을 선택하여 사용할 수 있지만
internWork 호출할때는 Intersection한 모든 요소들을 사용해야 컴파일 오류가 나지않는다.
Intersection
- 교차점
- 사거리
- 교집합