타입 가드
Type Guard
typeof
typeoffunction doSomething(x: number | string) {
if (typeof x === 'string') { // TypeScript는 이 조건문 블록 안에 있는 `x`는 백퍼 `string`이란 걸 알고 있습니다.
console.log(x.subtr(1)); // Error: `subtr`은 `string`에 존재하지 않는 메소드입니다.
console.log(x.substr(1)); // ㅇㅋ
}
x.substr(1); // Error: `x`가 `string`이라는 보장이 없죠.
}instanceof
instanceofin
in리터럴 Type Guard
null과 undefined (strictNullChecks)
null과 undefined (strictNullChecks)사용자 정의 Type Guards
Type Guard와 Callback
Last updated