Narrowing, guards and modules
Learn Narrowing, guards and modules (Module 2 · TypeScript, Angular) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.
Type narrowing, guards and assertions • Type narrowing : inside a check, the compiler knows more. After if (typeof x === 'string') , x is a string in that block. The checks that narrow are type guards : typeof, instanceof, in, equality, truthiness, and your own functions. A type assertion ( as ) simply tells the compiler "trust me"; it performs no runtime check.