0Pricing
TypeScript Academy · Lesson

Type Widening and Narrowing Mechanics

Learn how TypeScript widens types and when to prevent it.

What Is Type Widening?

Type widening is when TypeScript infers a broader type than you wrote. Assigning let x = "hello" widens to string, not the literal "hello".

let x = "hello"; // inferred: string, not "hello"
let y = 42;       // inferred: number, not 42

Widening with let vs const

Variables declared with const keep their literal type because they cannot be reassigned. let widens to the base type.

const a = "world"; // type: "world"
let   b = "world"; // type: string

All lessons in this course

  1. Type Widening and Narrowing Mechanics
  2. Contextual Typing: Inference from Context
  3. Freshness and Excess Property Checking
  4. const Assertions and as const
← Back to TypeScript Academy