Literal Types
A literal type allows exactly one value. "GET" is a type containing only the string "GET", and 200 is a type containing only that number. Combined with unions, literal types describe a fixed set of allowed values.
Detailed Explanation
Most types describe a category: string means any string at all. A literal type describes a single value. On its own that sounds useless — a variable that can only ever hold "GET" is not interesting. Literal types become useful the moment you union them.
type HttpMethod = "GET" | "POST" | "PUT" | "DELETE";
That one line replaces a comment saying "pass one of these four strings". The compiler now rejects "PATCH" and "get", your editor autocompletes the four options at every call site, and a switch over the value can be checked for exhaustiveness. None of that costs anything at runtime; the type disappears and a plain string is passed.
Literal types exist for strings, numbers, booleans, and bigints. boolean is internally the union true | false, which is why narrowing a boolean works the same way narrowing any union does.
Before literal unions became idiomatic, the options were string constants (no checking), integer codes (checked but unreadable), or enum (checked, readable, and it emits an object into your bundle). Literal unions give you checking and readability with zero emitted code, which is why most modern TypeScript reaches for them first and keeps enum for cases that need a runtime value to iterate over.
The thing you must understand to use them is widening. A literal type is fragile: TypeScript keeps it only where the value cannot change. const method = "GET" has type "GET" because nothing can reassign it. let method = "GET" has type string, because reassignment is legal and pinning the literal would make the variable useless. Object properties are mutable too, so { method: "GET" } infers as { method: string } even when the object is held in a const.
That widening is why a config object stops fitting a literal-typed parameter. Three fixes, in rough order of preference: annotate the object with the target type, append as const to freeze the literals, or annotate the specific property.
Literal types also appear in places you may not have noticed. A discriminated union works because each branch carries a different literal tag. keyof produces a union of literal string types. Template literal types build new literals by interpolating existing ones. Mapped types iterate over literal keys.
What do they cost? Very little at runtime and a moderate amount of maintenance: adding a fifth HTTP method means editing the union and then fixing every switch the compiler now reports as non-exhaustive, which is usually the behaviour you wanted. Very large literal unions, such as every CSS property name, do slow type-checking noticeably.
Code Examples
1. A union of literals turns a stringly-typed API into a checked one.
// 1. A union of literals turns a stringly-typed API into a checked one.
type FeatureFlag = "new-checkout" | "dark-mode" | "beta-search";
const enabled: Record<FeatureFlag, boolean> = {
"new-checkout": true,
"dark-mode": false,
"beta-search": true,
};
function isOn(flag: FeatureFlag): boolean {
return enabled[flag];
}
isOn("dark-mode"); // fine
isOn("darkmode");
// Error: Argument of type '"darkmode"' is not assignable to parameter of type 'FeatureFlag'.2. Widening: where the literal type survives and where it is lost.
// 2. Widening: where the literal type survives and where it is lost.
const direct = "POST"; // type "POST"
let mutable = "POST"; // type string — reassignment is possible
const wrapped = { method: "POST" }; // type { method: string } — properties are mutable
declare function send(method: "GET" | "POST"): void;
send(direct); // fine
send(mutable);
// Error: Argument of type 'string' is not assignable to parameter of type '"GET" | "POST"'.
send(wrapped.method); // same error, for the same reason3. Three ways to keep the literal on an object property.
// 3. Three ways to keep the literal on an object property.
type RequestSpec = { method: "GET" | "POST"; path: string };
const annotated: RequestSpec = { method: "POST", path: "/charges" }; // target type pins it
const frozen = { method: "POST", path: "/charges" } as const;
// type: { readonly method: "POST"; readonly path: "/charges" }
const narrowed = { method: "POST" as const, path: "/charges" };
// type: { method: "POST"; path: string } — only the one property is pinned
declare function dispatch(spec: RequestSpec): void;
dispatch(annotated);
dispatch(frozen); // readonly property modifiers do not block assignability here
dispatch(narrowed);Common Gotchas
`let` throws the literal away immediately
the widening happens at declaration, not at first use. If a value needs to keep its literal type, declare it with `const` or annotate it.
`as const` is deep and adds `readonly`
it freezes nested arrays and objects too, turning arrays into readonly tuples. That is usually what you want for config, but it will be rejected by a function that requires a mutable array.
Literal types are erased, so nothing validates at runtime
a value that arrives from JSON and is annotated as `FeatureFlag` is unchecked. Narrow it with a real runtime check before trusting the type.
Prefer a literal union over `enum` unless you need the runtime object
literal unions emit nothing, while `enum` emits a lookup object into your bundle and introduces its own assignability quirks.