Handbooks / TypeScript / Chapter 3
Narrowing
7 pages · ~5 min✓ Reviewed
Builds on Interfaces, Types and Unions. Next up: Generics.
Narrowing Mental Model
1. What narrowing is
- Narrowing refines a union inside a branch, using control-flow analysis of runtime checks
typeof x === 'string'narrows primitivestypeofgives: 'string' 'number' 'bigint' 'boolean' 'symbol' 'undefined' 'object' 'function'
2. Null and falsy traps
typeof nullis 'object', sotypeof x === 'object'keeps null untilx !== nullif (x)also drops valid0,''andNaN
| Check | Removes |
|---|---|
if (x) | null, undefined, 0, '', NaN |
x != null | null, undefined only |
x !== undefined | undefined only |
3. Reassignment and closures
- A closure made before the last assignment loses narrowing
- Assignment narrows to the assigned type:
let x: string | number = 'a'is a string - Since TS 5.4, closures created after the last assignment keep it
let id: string | undefined = load(); if (id) { setTimeout(() => id.length, 0); // error } id = undefined;
id is possibly undefined inside the callback
Built-in Narrowing Operators
1. instanceof
- instanceof checks the prototype chain, narrows to the class type
if (e instanceof Error) { e.message }
- Interfaces and type aliases are erased, so no
instanceof - Fails across realms (iframes): each has its own
Error
2. in and equality
'swim' in petkeeps union members that have (or may have)swim
Discriminated Unions
1. Shape of a Union
- Members share one tag field with a literal type
- Each member has its own tag value
type Shape = | { kind: 'circle'; r: number } | { kind: 'square'; side: number }; if (s.kind === 'circle') s.r; else s.side;
s: Shape; each branch sees one member
2. Narrowing and the Literal Rule
switch (s.kind)orifnarrows to exactly one member- Tag can be a string, number, boolean, null or undefined literal
- Plain
stringtag: narrowing fails
Exhaustiveness with never
1. The never leftover
- Each
caseremoves one member from the union - When all are handled,
defaultsees never - Nothing can reach it, so
shas no valid value
2. Enforcing it
function assertNever(x: never): never { throw new Error('Unexpected: ' + x) } switch (s.kind) { case 'circle': return 'c' case 'square': return 'q' default: return assertNever(s) }
default passes the leftover to a never parameter
- 1Union growsnew kind
- 2Leftovers is Triangle, not never
- 3Compile errorevery unhandled switch
Custom Guards and Assertion Functions
1. Type Guards
p is Fishis a type predicate- True branch narrows to Fish; false branch excludes Fish
- Return type
booleanalone does not narrow
function isFish(p: Fish | Bird): p is Fish { return 'swim' in p } if (isFish(p)) p.swim() // Fish else p.fly() // Bird
predicate is the contract
TypeScript trusts the predicate. A guard that returns true for everything silently gives unsound types.
Narrowing Cheat Sheet
1. Checks and when to pick them
| Check | Narrows | Pick for |
|---|---|---|
typeof | primitives, function | unknown input |
instanceof | class instances | class hierarchies |
in | property presence | object unions |
=== / == null | literals, null |
Check yourself
x is string[] | null. Why does if (typeof x === 'object') { x.length } fail to compile?
typeof nullis'object', sonullstays in the type- Check
x !== nullinstead
function f(n: number | undefined) { if (n) return n; return 'none' }. What does f(0) return?
'none': truthiness drops0,''andNaNtoo- Use
n !== undefinedwhen falsy values are valid