Literal widening
Concept and syntax
Inference preserves some literal information but widens it where mutation is expected. A const binding initialized to "tea" has literal type "tea"; a let binding initialized to it normally widens to string. A property of a const object can still be reassigned, so its string value usually widens. An explicit literal annotation or const assertion can preserve narrower information.
Working example
Compile standalone examples in a fresh directory with tsc --strict --target ES2022 --module commonjs --noEmitOnError main.ts, then node main.js.
const fixed = "tea";const onlyTea: "tea" = fixed;let label = "tea";label = "coffee";const order = { item: "tea" };order.item = "coffee";console.log(onlyTea, label, order.item);Expected output (one line per log):
tea coffee coffeeCommon mistake
Intentionally invalid; check this snippet separately. Do not run it.
Expected compiler diagnostic: TS2322. The mutable binding has inferred type string, not the single literal. Annotate it as "tea" if that is truly the contract.
Practice and recap
Compare let, const and a mutable property initialized with "ready". Test assignment to a variable typed only as "ready".
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Literal widening — knowledge check
Check the concept, apply it to a new choice, and interpret a compiler diagnostic.
Which binding normally retains the literal type "tea"?
Why can a const object property still widen?
What is the static type of label in the snippet?
let label = "tea";const onlyTea: "tea" = label;