1. Control Flow Analysis
If Statements
Most narrowing comes from expressions inside if statements, where different type operators narrow inside the new scope
typeof (for primitives)
const input = getUserInput();
// input string | number
if (typeof input === 'string') {
// input string
}
instanceof (for classes)
const input = getUserInput();
// input string | number[]
if (instance of Array) {
// input number[]
}
"property" in object (for object)
const input = getUserInput();
// input string | { error: ...}
if ('error' in input) {
// input { error: ...}
}