Type Guard로 안전함을 파악하기


Type Guard로 안전함을 파악하기

1. typeof Type Guard - 보통 Primitive 타입일 경우 function getNumber(value: number | string): number { value; // number | string if (typeof value === 'number') { value; //number return value; } value; // string return -1; } 2. instanceof Type Guard // 일반적인 예제 interface IMachine { name: string; } class Car implements IMachine { name: string; wheel: number; } class Boat implements IMachine { name: string; motor: number; } function getWhellOrMotor(machine: Car | Boat): number { if (machine instanceof Car)...


#TypeGuard #TypeScript #타입가드 #타입스크립트

원문링크 : Type Guard로 안전함을 파악하기