Type Widening

Noun · Development

Definitions

  1. A behavior in TypeScript's type inference where a literal type is automatically broadened to its base type — for example, a `let` variable initialized with `'hello'` is inferred as `string`, not the literal type `'hello'`. This is the opposite of narrowing and can be controlled with `as const` to preserve literal types.

    In plain English: When TypeScript automatically makes a type less specific than the actual value, like treating the exact text 'hello' as just 'any string.'

    Example: "The config object's keys got widened to `string` instead of staying as literal types — add `as const` to preserve them for the discriminated union."

Related Terms