Type Narrowing
In one line
Type narrowing is the process by which TypeScript refines a value's type to a more specific type within a code block, based on checks like typeof, instanceof, or truthiness.
In simple words
Type narrowing is TypeScript following your code's control flow to figure out a more specific type for a value. When you check a type with if, switch, or other conditionals, TypeScript narrows the type inside that block.
For example, if x is string | null, after if (x !== null), TypeScript narrows x to string — you don't need to assert it. This is called control flow based type narrowing.
Narrowing works with typeof, instanceof, in, truthiness checks, equality comparisons, and user-defined type guards. TypeScript tracks the narrowed type through if/else, switch, ternary operators, and even assignments.
Example
function greet(name: string | null) {
if (name === null) {
return "Hello, stranger";
}
// Here, name is narrowed to string
return `Hello, ${name.toUpperCase()}`;
}After the null check, TypeScript narrows name to string, allowing .toUpperCase().
Related terms
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.