Guided Notes
Type Narrowing & Casting
typeof, instanceof, in, equality and truthiness narrowing, type predicates, assertion functions, and casting with as — plus when casting is dangerous.
What narrowing is
Slide 1 / 4When a value has a union type, TypeScript narrows it inside checks that rule types out:
function len(x: string | string[]) { if (typeof x === "string") return x.length; // x: string here return x.join("").length; // x: string[] here }
Narrowing follows your control flow: if/else, switch, early return, &&, ||, ternaries.
