Skip to content

Type Widening

In one line

Type widening occurs when TypeScript generalizes a literal type (like 5) to its base type (like number) during type inference, typically for mutable variables.

In simple words

Type widening happens when TypeScript takes a specific literal type and makes it more general. For let x = 5, x is widened to number instead of staying as the literal 5, because let variables can be reassigned to any number.

For const x = 5, x stays as the literal type 5 because const cannot be reassigned. You can prevent widening with as const: let x = 5 as const gives x the type 5.

Widening affects arrays too: [1, 2, 3] is inferred as number[], not [1, 2, 3]. Use as const to get the tuple type readonly [1, 2, 3]. Understanding widening is important for exhaustiveness checking and literal type usage.

Example

typescript
let x = 5;       // type: number (widened)
const y = 5;     // type: 5 (literal)
let arr = [1, 2]; // type: number[] (widened)
const tuple = [1, 2] as const; // type: readonly [1, 2]

let widens literal types; const preserves them. 'as const' forces literal types.

Related terms

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary