Koskamo

Type narrowing and control flow analysis

Document v1 · published by Koskamo editorial · updated 2026-10-04

Original source

Narrowing is the process of refining types to more specific types than declared, driven by control flow analysis: TypeScript uses reachability-based flow analysis to narrow types as it encounters type guards and assignments, and a variable can be observed to have a different type at each point as control flow splits off and re-merges. typeof narrows using the runtime strings "string", "number", "bigint", "boolean", "symbol", "undefined", "object", and "function"; truthiness narrowing works through conditionals, &&, ||, and Boolean negations; equality narrowing uses switch statements and equality checks like ===, !==, ==, and !=; the in operator narrows unions on whether an object or its prototype chain has a property of a given name; and instanceof checks whether the prototype chain of a value contains Foo.prototype. When existing JavaScript constructs are not enough, a user-defined type guard is a function whose return type is a type predicate such as pet is Fish. For structured data, discriminated unions give each member a shared field like kind with a distinct string literal type, so comparing kind narrows to one member; when narrowing removes every possibility TypeScript uses the never type, and because never is assignable to every type but nothing except never is assignable to never, assigning the leftover to never in a default case gives exhaustiveness checking in a switch. Scope: TypeScript handbook type narrowing. Reviewed October 4, 2026.

Source and provenance

Original source
www.typescriptlang.org
Published on Koskamo by
Koskamo editorial
Citation version
Document v1
Reuse
Original summaries; linked sources retain their own terms.

The original source supplies the underlying information. Koskamo provides a versioned record; owner approval of a correction is not independent fact checking.