A type guard is a runtime check or predicate that narrows a value to a more specific TypeScript type. It is especially useful when handling values whose type is unknown, such as API responses, user input, or parsed JSON. section 3.12 section 3.7
Using typeof to narrow unknown
let vUnknown: unknown = 10; if (typeof vUnknown === "number") { let text: string = vUnknown.toString(); } The typeof check narrows vUnknown before the value is used. Without narrowing, assigning it directly or calling a method is not permitted. section 3.7
Interview points to articulate:
- Prefer unknown over any for uncertain values because unknown requires explicit checks, while any bypasses type checking and can cause runtime errors. section 3.7
- For deeply nested or unpredictable external data, combine partial validation, critical-field assertions, utility types, or schema validation libraries. section 3.7

