Type narrowing refines a value’s possible types through typeof, instanceof, control-flow analysis, user-defined type guards, and discriminated unions. It matters in interviews because it demonstrates practical type safety in evolving TypeScript codebases. section 3.9
Be ready to explain these interview-relevant distinctions:
- Use typeof and instanceof for basic narrowing, then apply control-flow analysis to track refined types across branches. section 3.9
- Create user-defined type guards and handle discriminated unions when basic checks are insufficient. section 3.9
- Contrast union types with generics: unions may require narrowing, while generics preserve relationships between inputs and outputs. section 3.8
- Discuss exhaustive checks and never when designing APIs that must account for every allowed case. section 3.9
Table 3.7 Key features of type safety and tools
|
Topic
|
Fundamentals
|
Applied skills
|
Advanced
|
|
Type narrowing
|
Use
typeof,
instanceof, and control flow analysis to narrow types.
|
Create user-defined type guards, handle discriminated unions.
|
Architect safe APIs with exhaustive type checks, leverage never for unreachable code.
|
|
Type assertions & casting
|
Use
as and angle-bracket syntax for casting.
|
Refactor code to reduce reliance on assertions and avoid unsafe casts.
|
Audit large codebases for unsafe assertions; enforce strict type safety policies.
|
|
Strictness & config
|
Enable
strict mode, explain key compiler config options.
|
Configure strictness for projects, resolve common strictness errors.
|
Architect enterprise-scale codebases with strictest settings, automate config validation across teams.
|
|
JavaScript interop & declaration files
|
Use
.d.ts files, import JS modules, explain
any.
|
Write custom declaration files, leverage
unknown and
never for safer interop.
|
Integrate with complex JS libraries; automate API type generation from OpenAPI/GraphQL schemas.
|
|
Tooling & build
|
Set up
tsc, configure a basic
tsconfig.json.
|
Integrate with bundlers (Webpack, Vite), enable source maps and incremental builds.
|
Architect monorepos, automate type checking in CI/CD pipelines, enforce code quality gates.
|
Use the type-safety comparison to distinguish foundational narrowing techniques from applied guards, discriminated unions, and advanced exhaustive checks. section 3.9

