Sub topic 7
Literal types and widening
This chapter assumes you know basic TypeScript syntax, union types, and how const and let differ in JavaScript. It explains why a value that looks right can fail to be assigned to a union of literals, and how to fix it.
Literal types are small sets
A literal type contains exactly one value. The type "fast" holds only the string "fast", 42 holds only the number 42, and true holds only true. Under the types-as-sets model, "fast" is a subset of string. That makes "fast" assignable to string, but not the reverse.
A union of literals names a small closed set:
type Mode = "fast" | "slow";
const ok: Mode = "fast"; // OK: "fast" is a member of the union
const bad: Mode = "medium"; // Error: not a memberWidening: when a literal becomes a general type
The compiler must pick a type for each expression. Where a value can change later, it chooses the general type (string, number, boolean) instead of the literal. This is called widening. Where the value cannot change, it keeps the literal type.
type Mode = "fast" | "slow";
let widened = "fast"; // type: string
const fixed = "fast"; // type: "fast"
const config = { mode: "fast" };
const m1: Mode = fixed; // OK
const m2: Mode = config.mode; // Error: string is not assignable to Mode
const config2 = { mode: "fast" } as const;
const m3: Mode = config2.mode; // OKThe rules behind this example:
- A
constvariable cannot be reassigned, so its type keeps the literal. - A
letvariable can be reassigned, so its type widens to the primitive. - Properties of an object literal are mutable by default, so each one widens, even when the object itself is
const. Theconstonly stops you from reassigningconfig. It does not stopconfig.mode = "slow"orconfig.mode = "anything".
The error on m2 is a consequence of that last rule. The compiler checks the declared type of config.mode, which is string, and does not look at the initial value "fast". A string can hold values outside Mode, so it is not assignable.
Fresh and widening literal types
Internally the compiler tracks two kinds of literal type. A fresh (widening) literal comes straight from an expression such as "fast". When it is stored in a mutable location, freshness lets it widen. A regular (non-widening) literal, such as the one you write in an annotation (let x: "fast"), never widens.
This explains a case that surprises people:
const a = "fast"; // fresh literal "fast"
let b = a; // b is string: the fresh literal widens in a mutable location
const c: "fast" = "fast"; // annotated, so non-widening
let d = c; // d is "fast": the annotation prevents wideningYou do not need these names in daily work. The practical point is that a const initialized from a bare literal widens again when copied into a let, while an explicit annotation pins the type.
Where widening happens
| Situation | Resulting type |
|---|---|
| `const x = "a"` | `"a"` |
| `let x = "a"` | `string` |
| `{ k: "a" }` property | `string` |
| `["a", "b"]` array elements | `string[]` |
| `{ k: "a" } as const` | `{ readonly k: "a" }` |
| `["a", "b"] as const` | `readonly ["a", "b"]` |
| Return value of `function f() { return "a"; }` | `string` |
| `const f = () => "a" as const` | `() => "a"` |
Function return types widen too, which is a common source of errors when a function result is passed to a parameter typed with a literal union.
Contextual typing prevents widening
When an expression appears where the compiler already knows the target type, it uses that type as context and keeps the literal. This is why a direct assignment works but a detour through an untyped variable does not.
type Mode = "fast" | "slow";
function run(mode: Mode) {}
run("fast"); // OK: the parameter type is the context
const options = { mode: "fast" };
run(options.mode); // Error: options.mode was widened to string
const typed: { mode: Mode } = { mode: "fast" };
run(typed.mode); // OK: the annotation supplied the contextThe same applies to array elements, return statements in a function with a declared return type, and arguments to generic functions whose type parameter is constrained to a primitive.
Fixing a widening error
You have four options. Pick the one that matches what you want to say.
- Annotate the variable or property with the target type. This is the clearest choice and documents intent.
- Use
as conston the initializer. This makes the whole structure readonly and literal, which is right for fixed lookup data. - Use
satisfies(TypeScript 4.9 and later). It checks the value against a type while keeping the narrower inferred type. - Write the literal assertion on one value, such as
{ mode: "fast" as const }, when only one property needs it.
type Mode = "fast" | "slow";
const a = { mode: "fast" } satisfies { mode: Mode };
const m: Mode = a.mode; // OK: "fast" was kept because satisfies gave contextA type assertion such as "fast" as Mode also compiles, but it silences the checker instead of informing it. Prefer the options above.
Other literal kinds
Number, boolean, and bigint literals widen the same way. let n = 1 is number, and const n = 1 is 1. Template literal types such as ` ${number}px also keep their shape under const but widen to string in a mutable location. Enum member literals widen to their enum type in let` declarations.
The boolean type is itself the union true | false, so boolean is a literal union already. That is why narrowing on a boolean can leave true or false behind.
Common mistakes
- Expecting
const obj = { mode: "fast" }to freezemode. It freezes only the binding. - Passing a function result that returns a bare literal to a literal-typed parameter, without declaring the return type.
- Using
as constand then being surprised byreadonlyerrors. - Reaching for
as Modewhen an annotation would have been checked.
Exercise
Given type Dir = "up" | "down"; const step = { dir: "up" }; function move(d: Dir) {}, why does move(step.dir) fail, and name two ways to fix it.
Show answer
The property dir in an object literal is mutable, so its type widens to string, and string is not assignable to Dir. Fix it by annotating the variable (const step: { dir: Dir } = { dir: "up" }) or by using as const on the initializer (const step = { dir: "up" } as const). satisfies { dir: Dir } also works.
Exercise
What are the types of x and y in const a = "go"; let x = a; const y = a;?
Show answer
x is string, because copying a fresh literal into a let widens it. y is "go", because a const keeps the literal type.