Type System Deep Dive
Type Narrowing (Control Flow Analysis)
Master Control Flow Analysis in TypeScript, truthiness narrowing, equality narrowing, assignment narrowing, and strict type refinement.
Last Updated: July 29, 2026
•
10 min read
Type Narrowing is the process by which TypeScript refines broad types (like string | number | null) into narrower, more specific types based on runtime conditionals.
TypeScript uses Control Flow Analysis (CFA) to analyze your code's execution paths (branches, returns, loops, throws) and track type refinements at every line of code.
1. Mechanisms of Control Flow Analysis
TypeScript automatically tracks variable assignments and conditional checks across execution paths:
2. Types of Narrowing
1. Equality Narrowing (===, !==, ==, !=)
2. Truthiness Narrowing (if (val))
Filtering out null, undefined, 0, "", false, and NaN:3. Early Return / Unreachable Code Narrowing
Control flow analysis recognizesreturn, throw, and break statements to eliminate types in subsequent lines:4. Assignment Narrowing
Assigning a specific value to a union variable narrows its type immediately:3. Interactive Code Playground
Test control flow analysis narrowing below:
4. Summary Table
| Narrowing Technique | Guard Example | Types Eliminated |
|---|---|---|
| Equality Check | if (x === "admin") | All non-matching literals/types |
| Truthiness Guard | if (val) | null, undefined, 0, "", false |
| Early Return | if (!x) return; | Falsy types for remaining function body |
typeof Guard | if (typeof x === "number") | All non-number union arms |