Skip to content
jshacks.dev
TypeScript

Type Widening and Narrowing of Literals

Why const x = "a" has a literal type but let x = "a" is a string, how object properties widen, and how as const, satisfies and annotations control it.

8 min read
Summarize in AI
Flat illustration of an editor window full of code, with the TypeScript, JavaScript and npm logos.

The same string literal gets different types depending on where you put it. Write const a = "hi" and the type of a is the literal "hi". Write let b = "hi" and the type of b is plain string. Put the literal in an object and its property is string even when the object is a const. These are not three separate rules. They are one mechanism, called literal widening, and understanding it explains most of the surprising errors people hit when they pass a variable to a function that wants a union of strings. This post describes how literal types are inferred, when they widen to their general type, how to keep them narrow, and how to narrow them again with checks. It describes TypeScript 6.x, and the behaviour is identical in the native compiler preview that TypeScript 7 is built on. The reference is the handbook chapter on everyday types and the section on literal inference.

Literal types and their general types

A literal type is a type with exactly one value. The type "hi" accepts only the string "hi". The type 42 accepts only 42, and true accepts only true. Each has a general type that contains it: string, number and boolean. A value of a literal type can always be used where the general type is expected, since "hi" is a string. The reverse is not true, and that asymmetry is what causes the errors.

The question the compiler has to answer on every declaration is which of the two to infer. The answer depends on whether the variable can change.

const keeps the literal, let widens it

A const binding can never be reassigned, so its narrowest type is safe, and the compiler gives it the literal type. A let binding can be reassigned to any other string, so the compiler widens the literal to the general type.

basic.tsTypeScript
const a = "hi";
let b = "hi";

b = "bye";
a = "bye";
Text
basic.ts(5,1): error TS2588: Cannot assign to 'a' because it is a constant.

The assignment to b on line 4 is fine, because b is a string. The assignment to a is an error for the ordinary reason that constants cannot change, and the type of a is "hi" regardless. You can see the types by trying to use them where a literal is needed.

basic.tsTypeScript
function greet(word: "hi" | "bye"): void {
  console.log(word);
}

const a = "hi";
let b = "hi";

greet(a);
greet(b);
Text
basic.ts(9,7): error TS2345: Argument of type 'string' is not assignable to parameter of type '"hi" | "bye"'.

The call with a compiles and prints hi. The call with b fails, because b was widened to string when it was declared, even though its value is "hi" at that moment. The compiler does not track the current value of a let variable in its declared type, since the variable might be reassigned at any point.

Widening literal types

There is a subtlety underneath. The type of const a = "hi" is not just "hi". It is a special form called a widening literal type. It behaves as "hi" when you use a directly, and it widens to string if you copy it into a mutable location.

widening.tsTypeScript
const a = "hi";
let b = a;

const c: "hi" = "hi";
let d = c;

b = "bye";
d = "bye";
Text
widening.ts(8,1): error TS2322: Type '"bye"' is not assignable to type '"hi"'.

The variable b receives the widening literal from a and widens to string, so assigning "bye" works. The constant c has an explicit annotation, which produces a non-widening literal type, and d inherits "hi" exactly. That is why the error is reported on d. The only difference between a and c is the annotation, and it changes what a let copy ends up with. This is an implementation detail that rarely matters until it does, and the fix is always to write the type you mean on the variable that needs it.

Object and array properties widen

Properties are mutable by default, so the compiler widens them even inside a const object.

props.tsTypeScript
function run(mode: "fast" | "slow"): void {
  console.log(mode);
}

const options = { mode: "fast" };

run(options.mode);
Text
props.ts(7,5): error TS2345: Argument of type 'string' is not assignable to parameter of type '"fast" | "slow"'.

The binding options is constant, but the property mode is not, and the compiler infers { mode: string }. This is the most common form of the problem in real code, usually with a config object defined at module level and passed to an API that wants a union.

Arrays follow the same rule. const sizes = [1, 2, 3] is number[], since the elements can be replaced.

Keeping literals: annotations, as const and satisfies

There are three tools for stopping the widening, and each fits a different situation.

The first is a type annotation. If you tell the compiler the type, it uses it, and the literal is checked against it.

annotated.tsTypeScript
function run(mode: "fast" | "slow"): void {
  console.log(mode);
}

const options: { mode: "fast" | "slow" } = { mode: "fast" };

run(options.mode);
Text
fast

The object literal is contextually typed by the annotation, so "fast" is accepted as a member of the union and the property keeps the union type. The call compiles and prints fast. The cost is that you must write the type out, and the property now has the full union and not just "fast".

The second tool is as const. It marks the whole expression as constant, which makes every property readonly and every literal narrow.

asconst.tsTypeScript
function run(mode: "fast" | "slow"): void {
  console.log(mode);
}

const options = { mode: "fast", retries: 3 } as const;

run(options.mode);
options.retries = 4;
Text
asconst.ts(8,9): error TS2540: Cannot assign to 'retries' because it is a read-only property.

The call on line 7 compiles, since options.mode has type "fast". The assignment on line 8 is rejected because as const made the property readonly, with the literal type 3. Arrays become readonly tuples, so [1, 2] as const has type readonly [1, 2]. This is the right tool when the object really is a constant table, such as a list of routes or role names, and you want the literal types to flow into other types.

The third is satisfies, added in TypeScript 4.9. It checks the value against a type but keeps the narrower inferred type of the value.

satisfies.tsTypeScript
type Options = { mode: "fast" | "slow"; retries: number };

const options = { mode: "fast", retries: 3 } satisfies Options;

const m: "fast" = options.mode;
const r: 3 = options.retries;
Text
satisfies.ts(6,7): error TS2322: Type 'number' is not assignable to type '3'.

Here mode stays "fast" because the satisfies check provides a contextual type that asks for a literal, so the first assignment is fine. The retries property is number, because the target type says number, and nothing asked for a literal. The result is that you get validation against Options and still keep the literal where the target type invites it. Add as const as well, written as { ... } as const satisfies Options, when you want everything narrow and also validated.

Function results widen too

Return types are inferred from the returned expressions, and literals are widened there as well.

returns.tsTypeScript
function pick() {
  return "a";
}

const x: "a" = pick();
Text
returns.ts(5,7): error TS2322: Type 'string' is not assignable to type '"a"'.

The inferred return type is string. To get the literal, annotate the return type as "a", or return "a" as const. For functions that return a literal from a generic input, use a type parameter with a const modifier, such as function f<const T>(x: T), which asks the compiler to infer the narrowest type for the argument, the same effect as writing as const at the call.

Narrowing literals again

Widening goes from specific to general at a declaration. Narrowing goes the other way, from general to specific, through checks in the code. A variable typed as a union of literals narrows as you compare it.

narrow.tsTypeScript
type Mode = "fast" | "slow" | "off";

function describe(m: Mode): string {
  if (m === "off") {
    return "disabled";
  }
  const rest: "fast" | "slow" = m;
  return "running " + rest;
}

console.log(describe("fast"));
Text
running fast

After the early return for "off", the compiler removes that member, so m is "fast" | "slow" and the assignment is accepted. The same thing happens with switch statements. If your declared type is plain string, the equality check narrows to the literal in the true branch, and the false branch stays string, because the compiler cannot subtract one value from an infinite set.

A related trick is a type guard that checks membership in a readonly tuple and narrows a string to the union of its literals. It is written as a function returning value is Mode, and the check inside must be correct, since the compiler trusts it.

Practical guidance

When you build a config object that will be passed to a typed API, annotate it with the API's type, or use satisfies. When you build a constant table that the rest of your types derive from, use as const. When a function returns one of a few strings, annotate the return type with the union, so that callers get a useful type instead of string.

If an error says string is not assignable to a union of literals, look for the place where the value was declared. The widening happened there, not at the call.

Common mistakes

The first is declaring a mutable variable and expecting its current literal value to count as its type. The second is using as const on an object that you later need to mutate, and then fighting the readonly errors. The third is annotating with the union and losing the specific literal that satisfies would have kept. The fourth is casting with as "fast" to get past the error, which asserts without checking and hides a mistake if the value ever changes.

When this does not apply

Widening is about inference. If every declaration carries an explicit annotation, there is nothing to widen, and the types are exactly what you wrote. For values that come from outside the program, the literal type is a claim and not a fact, so validate the string at run time before narrowing it with a guard.

Share

Keep reading

Flat illustration of an editor window full of code, with the TypeScript, npm and Node.js logos.
TypeScript

Type Inference in TypeScript

TypeScript works out most types for you. Learn how inference treats let and const, arrays, return values and callbacks, and when to write a type yourself.

7 min read
Flat illustration of an editor window full of code, with the TypeScript, Node.js, npm, Vite and JavaScript logos.
TypeScript

What Is TypeScript, and Why Use It?

TypeScript adds static types to JavaScript and checks them before your code runs. Learn what it is, what it catches, what it costs and when to use it.

6 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.