liveBook · Manning

Type narrowing

Type narrowing is the compile-time refinement of a value's possible static types based on control-flow checks, type guards, or equivalent analysis.

How 2 Manning books explain it

Essential TypeScript 5, Third Edition

Type narrowing is the compile-time process of restricting an expression's possible types to a more specific type.

How it is used
Uses assertions and guards to select usable members from union-typed values.
How deep it goes
Explains assertions, property checks, and instanceof guards with code, but stops short of broader control-flow narrowing rules.
Examples
Demonstrates tax formatting, mixed Person and Employee arrays, constructor-function shapes, and Person-or-Product collections.
What you can do after
Apply it via TypeScript assertions and guards to access members specific to a selected union type.

Read how Essential TypeScript 5, Third Edition explains type narrowing →

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.

How it is used
Applies runtime checks and control-flow reasoning to maintain type safety across diverse TypeScript codebases and interview scenarios.
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 you can do after
Diagnose and implement safer TypeScript branches, guards, unions, and exhaustive API checks.

Read how Acing the Frontend Interview explains type narrowing →

Also covered in

What to know first

Related concepts

Questions about type narrowing

What is type narrowing?

Type narrowing is the compile-time refinement of a value's possible static types based on control-flow checks, type guards, or equivalent analysis.

Which Manning books explain type narrowing?

Essential TypeScript 5, Third Edition and Acing the Frontend Interview.

What should I know before learning type narrowing?

Manning books that teach type narrowing usually introduce any type first.

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.