liveBook · Manning

Type narrowing

As explained in Acing the Frontend Interview

Type narrowing is the refinement of a value's static type within a control-flow branch based on runtime checks or other type guards.

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

How Acing the Frontend Interview covers it

How deep it goes
Progresses from typeof and instanceof to custom guards, discriminated unions, exhaustive checks, and never.
Examples
Uses typeof, instanceof, user-defined guards, discriminated unions, exhaustive checks, and never as progression examples.
What it assumes
Assumes TypeScript static typing and basic union types while extending practice toward strict type-safety policies.
What you can do after
Diagnose and implement safer TypeScript branches, guards, unions, and exhaustive API checks.
How it connects
Connects type narrowing with union types, generics, control-flow analysis, type guards, discriminated unions, and type assertions.

Also explained in

Compare how 2 books explain type narrowing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Acing the Frontend Interview. The section links point to the parts of the book it draws on.