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 42Widening 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: stringAll lessons in this course
- Type Widening and Narrowing Mechanics
- Contextual Typing: Inference from Context
- Freshness and Excess Property Checking
- const Assertions and as const