TS Custom Type Guard
Static Declaration on Type
To define a type guard, you simply need to define a function whose return type is a type predicate
function isFish(pet: Fish | Bird): pet is Fish { return (<Fish>pet).swim !== undefined; }
Type Predicate Type
function isFish(pet: Fish | Bird): pet is Fish { return (<Fish>pet).swim !== undefined; }
- if
- typeof
- instanceof
- ===
- in
interface A { x: number } interface B { y: string } function doStuff(q: A | B) { if ('x' in q) else }

Seonglae Cho![[TypeScript] Advanced Types(고급 타입)](https://images.velog.io/post-images/zeros0623/9ef11850-31df-11ea-a94f-ab0e05c4904a/TypeScriptImage.jpeg)