유니언 타입은 하나의 변수가 여러 타입을 가질 수 있는 가능성을 표시하는 것입니다.
타입스크립트에서는 유니언 타입을 위한 파이프 연산자(|)가 있습니다.
let strOrNume : string | number = 'hello';
strOrnum = 123;
let strOrNum: string | number = 'hello';
strOrNum = 123;
if(typeof strOrNum === 'number){
strOrNum.toFixed();
}
타입 사이에만 | 연산자를 쓸 수 있는 것이 아니라 타입 앞에서도 사용할 수 있습니다.
type union1 = | string | boolean | number | null;
//여러줄에서 유니언을 표기하고 싶을 때 종종 사용
type union2 =
| string
| boolean
| number
| null;