Explain it simply
Explain it simply
The type string means "any text at all", while the literal type "GET" means "exactly the three letters GET". It is like the buttons in a lift: if there are only floors 1, 2 and 3, you cannot press floor 7. A literal union such as "GET" | "POST" is the list of allowed buttons.
The catch is that TypeScript often widens literals to a broader type like string. as const says "remember these values exactly as written", and satisfies says "check that this value fits the shape, but do not change its type".
Problem → Why → Solution
The problem: an annotation erases the keys
Suppose the app's route config is annotated as Record<string, Route>. The shape is checked, but the annotation replaces the variable's type with Record<string, Route>, so the compiler forgets that the keys are "home" and "account". A typo like routes.acount is not an error and quietly returns undefined, and path is just string instead of "/account".
type Route = { path: string; secure: boolean };
const routes: Record<string, Route> = {
home: { path: "/", secure: false },
account: { path: "/account", secure: true },
};
const link = routes.acount.path; // no compile error: any string key is allowed
// At runtime: TypeError, cannot read properties of undefined (reading 'path')Why does this happen?
An annotation declares "this variable's type is exactly this", which throws away the more precise information in the value. Removing the annotation brings the keys back, but then nothing checks the shape. satisfies resolves this trade-off: the value is checked, and its precise type is kept.
Core concept: widening, as const and satisfies
A literal type allows exactly one value: "GET", 42, true. Combine literals with a union and you get a list of choices, type Method = "GET" | "POST";, where the compiler catches "DELETE" or a misspelling like "get". Number literals work the same way: type DiceRoll = 1 | 2 | 3 | 4 | 5 | 6; rejects 7 and 0. In fact boolean itself is just the union true | false.
When TypeScript infers a type, it asks whether the value could change later. const exact = "GET"; can never be reassigned, so its type is "GET". let loose = "GET"; might hold another string later, so its type is widened to string. Object properties can change too, so in const request = { method: "GET" }; the method property is string, and passing it where a Method is required is an error.
as const stops widening at every level. It keeps every value inside an object or array as a literal type, makes every property readonly, and turns arrays into readonly tuples: { method: "GET" } as const has type { readonly method: "GET" }. It works purely at compile time and emits no JavaScript.
The satisfies operator, available since TypeScript 4.9, checks that a value matches a type without changing the value's inferred type. A mismatch such as secure: "yes" is reported immediately, and when it matches, every key and value type is kept. Combined as as const satisfies Type, the literals are kept too. Do not confuse it with as Type, an assertion that tells the compiler "trust me" and does not even catch a missing property.
| Form | Checks the shape? | Resulting type |
|---|---|---|
| const x: T = value | yes | T (precision lost) |
| const x = value as T | not fully | T |
| const x = value satisfies T | yes | the value's own type |
- A literal is a one-value type
- Changeable values widen to string
- as const blocks widening
- satisfies checks, keeps the type
An annotation squashes the type; satisfies only inspects it.
General pattern
type Choice = "first" | "second"; // literal union
const fixed = { key: "value" } as const; // readonly, literal types
const config = {
name: { field: "value" },
} satisfies Record<string, Shape>; // checked, type kept
const VALUES = ["a", "b"] as const;
type Value = (typeof VALUES)[number]; // "a" | "b"A literal union lists the choices, and as const keeps literals. satisfies checks a value against a shape, and (typeof VALUES)[number] turns the elements of a const array into a union type.
Example: a checked, precise route config
This example rewrites the route config from the problem with as const satisfies. The shape is still checked against Route, and the key names and path literals are kept. A type check on its own prints nothing, so the console.log lines print values that let you see what was kept.
type Route = { path: string; secure: boolean };
const routes = {
home: { path: "/", secure: false },
account: { path: "/account", secure: true },
} as const satisfies Record<string, Route>;
type RouteName = keyof typeof routes; // "home" | "account"
function linkTo(name: RouteName): string {
const route = routes[name];
return route.secure ? `${route.path} (login required)` : route.path;
}
const accountPath = routes.account.path; // type: "/account"
console.log(accountPath);
console.log(linkTo("home"));
console.log(linkTo("account"));Code breakdown
- Line 6
} as const satisfies Record<string, Route>;as const keeps the literals, and satisfies checks that every route has path: string and secure: boolean. Forget secure and the error appears right on this object.
- Line 8
type RouteName = keyof typeof routes;Because there is no annotation, the key names survive and RouteName is "home" | "account". With a Record<string, Route> annotation it would just be string.
- Line 10
function linkTo(name: RouteName): string {A misspelled call such as linkTo("acount") is now a compile error, which is exactly the bug from the problem section.
- Line 15
const accountPath = routes.account.path;accountPath has the literal type "/account", not string, thanks to as const.
/account
/
/account (login required)Widening in practice
A let variable and a plain object property are widened to string, so they cannot be passed as a Method (uncomment the two lines to see the errors). const and as const keep the literal.
type Method = "GET" | "POST";
function send(method: Method, url: string): string {
return `${method} ${url}`;
}
let loose = "GET"; // string (let can change later)
const exact = "GET"; // "GET"
const request = { method: "GET" }; // { method: string }
const frozen = { method: "GET" } as const; // { readonly method: "GET" }
// send(loose, "/a"); // Error: string is not assignable to Method
// send(request.method, "/b"); // Error: same reason
console.log(send(exact, "/users"));
console.log(send(frozen.method, "/orders"));
console.log(loose.length);When to reach for satisfies
Whenever you want a shape checked but still need the exact keys and values afterwards, as with config objects, lookup tables and themes, consider satisfies instead of an annotation.
In real projects
Where this shows up in real projects
Literal unions appear wherever the choices are fixed: HTTP methods, order statuses ("pending" | "paid" | "shipped"), button variants ("primary" | "secondary") and design-system sizes. A React prop typed size: "sm" | "md" | "lg" makes the editor autocomplete exactly the allowed values.
as const satisfies is common for config objects: route tables, feature flags, i18n message keys, permission maps and theme colour tokens. Deriving type Role from an array like ROLES = ["admin", "editor", "viewer"] as const lets you use the array at runtime, for example to render a dropdown, and the type at compile time, so there is only one list to maintain: adding a role happens in one place.
Common mistakes
Common mistakes
1. Using as Type instead of satisfies
const home = { path: "/" } as Route; compiles even though secure is missing, because an assertion accepts types that merely overlap. When you want the value checked, use satisfies Route or a : Route annotation, and keep as for the rare cases where you truly know more than the compiler.
2. Thinking as const freezes the object at runtime
as const only tells the compiler that values are readonly; nothing of it remains in the emitted JavaScript, so code that goes through any, or plain JavaScript, can still change the property. Add Object.freeze() when you need runtime protection.
3. Expecting a literal type from let
let method = "GET"; followed by send(method, url) fails with "Argument of type 'string' is not assignable to parameter of type 'Method'". Use const if it never changes, or give it the type yourself: let method: Method = "GET";.
Best practices
Best practices
- Write a list of choices once in an as const array and derive the type from it, instead of maintaining two lists.
- Prefer satisfies over an annotation for config objects, so the keys and literals stay usable.
- In code review, question every as Type: should it be satisfies or an annotation instead?
Mini challenge
Mini challenge: a log level config
Define the log levels once in an as const array, derive a type from it, and build a style config that is checked with satisfies.
- Derive type Level from LEVELS = ["debug", "info", "warn", "error"] as const
- The styles object has { label, enabled } for every level and is checked with satisfies Record<Level, LevelStyle>
- shouldLog(level, minimum) is true only if the level is enabled and not below minimum
- Try every level with minimum "info" and print the result
const LEVELS = /* TODO: the four levels, kept as literals */;
type Level = /* TODO: derive the union from LEVELS */;
type LevelStyle = { label: string; enabled: boolean };
const styles = {
// TODO: one entry per level, e.g. debug: { label: "DBG", enabled: false }
} /* TODO: check it against Record<Level, LevelStyle> */;
function shouldLog(level: Level, minimum: Level): boolean {
// TODO: enabled, and position in LEVELS >= position of minimum
}
// TODO: loop over LEVELS and print "<label> logged" or "<label> skipped"Expected: DBG skipped INF logged WRN logged ERR logged
Show hint
(typeof LEVELS)[number] gives the union of all element types. If you leave a level out, satisfies reports an error like "Property 'warn' is missing".
Show solution
const LEVELS = ["debug", "info", "warn", "error"] as const;
type Level = (typeof LEVELS)[number]; // "debug" | "info" | "warn" | "error"
type LevelStyle = { label: string; enabled: boolean };
const styles = {
debug: { label: "DBG", enabled: false },
info: { label: "INF", enabled: true },
warn: { label: "WRN", enabled: true },
error: { label: "ERR", enabled: true },
} satisfies Record<Level, LevelStyle>;
function shouldLog(level: Level, minimum: Level): boolean {
return styles[level].enabled && LEVELS.indexOf(level) >= LEVELS.indexOf(minimum);
}
for (const level of LEVELS) {
const verdict = shouldLog(level, "info") ? "logged" : "skipped";
console.log(`${styles[level].label} ${verdict}`);
}Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- A literal type allows one value, and unions like "GET" | "POST" list the allowed choices.
- let variables and object properties widen to string; as const keeps literals and readonly at every level.
- satisfies checks the shape and keeps the value's precise type, while an annotation replaces it.
- as Type is not a check, and (typeof ARR)[number] derives a union from a const array.
TypeScript Handbook — TypeScript