typescript
[Typescript]유니온 타입, 인터섹션 타입(Union, Intersection)
슬래기
2022. 6. 21. 23:55
Union
유니온 타입(Union Type)이란 자바스크립트의 OR 연산자(||)와 같이 'A' 이거나 'B'이다 라는 의미의 타입이다.
//Union Types : OR
//원하는 타입을 만들때 domain같은 역할
type Direction = 'left' | 'right' | 'up ' | 'down';
function move (directionn:Direction){
console.log(directionn);
}
move('down'); //down
Intersection
인터섹션 타입(Intersection Type)은 여러 타입을 모두 만족하는 하나의 타입을 의미한다.
//모든것을합한 성격 and &
type Student = {
name: string;
score : number;
}
type Worker = {
employeeId : number;
work : ()=> void;
}
//모든것에 접근 가능
function internWork(person: Student & Worker){
console.log(person.name,person.employeeId,person.work());
}
internWork({
name : 'kim',
score:1,
employeeId:1,
work : ()=>{}
})