Sub topic 16
Validating without widening using satisfies
This chapter covers the satisfies operator, which checks a value against a type without changing the type TypeScript infers for it. It assumes you know how inference works for const and let, how literal types widen, how union types and narrowing work, and what as const does. It also assumes you use TypeScript 4.9 or later, where satisfies was added.
The problem: annotate or lose validation
You often want two things at once. You want the compiler to confirm that an object has the right shape, and you want to keep the precise type of each property. Before satisfies, you had to pick one.
type Palette = Record<string, string | number[]>;
// Option 1: annotation. Validated, but widened.
const annotated: Palette = {
red: [255, 0, 0],
green: "#00ff00",
};
// annotated.red.map((c) => c / 255);
// Error: Property 'map' does not exist on type 'string | number[]'.
// Option 2: no annotation. Precise, but nothing is checked.
const unchecked = {
red: [255, 0, 0],
green: "#00ff00",
blue: 42, // not a valid palette entry, and no error here
};An annotation replaces the inferred type with the annotated one. After that, the compiler forgets that red is an array and green is a string. Without the annotation, nothing tells you that blue: 42 breaks the palette rules.
What satisfies does
expression satisfies Type checks that the expression is assignable to Type. It then keeps the expression's own inferred type.
type Palette = Record<string, string | number[]>;
const colors = {
red: [255, 0, 0],
green: "#00ff00",
} satisfies Palette;
const normalized = colors.red.map((channel) => channel / 255);
const hex = colors.green.toUpperCase();
console.log(normalized, hex);Here colors.red is number[] and colors.green is string. If you add blue: 42, the compiler reports an error on that property, because number is not assignable to string | number[].
The three approaches compare as follows:
| Approach | Checks the value | Resulting type of the variable |
|---|---|---|
| `const x: T = value` | Yes | `T` (the annotation wins) |
| `const x = value` | No | Inferred from the value |
| `const x = value satisfies T` | Yes | Inferred from the value |
| `const x = value as T` | Only loosely | `T` |
How it works internally
The checker does two things with the expression.
- It uses
Tas the contextual type while inferring the expression. This is the same mechanism that types callback parameters. Properties get checked against the matching part ofT, and literals are kept narrow whenTexpects literal types. - It verifies that the inferred type is assignable to
T. Then it discardsTand uses the inferred type as the type of the whole expression.
The next diagram shows where the types go.
Catching mistakes that a loose annotation hides
With a Record<string, ...> annotation, any key is accepted when you read it. With satisfies, the variable keeps its real keys, so a misspelled key is an error.
type Route = { path: string; method: "GET" | "POST" };
const routes = {
home: { path: "/", method: "GET" },
save: { path: "/save", method: "POST" },
} satisfies Record<string, Route>;
console.log(routes.home.method); // type is "GET", not "GET" | "POST"
// routes.hmoe; // Error: Property 'hmoe' does not existTwo details matter here:
- If you wrote
method: "PUT", the error would be reported on that property:Type '"PUT"' is not assignable to type '"GET" | "POST"'. routes.home.methodis the literal"GET". The contextual type"GET" | "POST"tells the compiler to keep the literal instead of widening it tostring.
Exact key sets
Pair satisfies with a union of keys to require every key and reject extras.
type Theme = "light" | "dark";
const labels = {
light: "Light mode",
dark: "Dark mode",
} satisfies Record<Theme, string>;
// Removing "dark" is an error: property 'dark' is missing.
// Adding "contrast" is an error: object literal may only specify known properties.
console.log(labels.light);This gives you the same exhaustiveness safety as the never check in a switch, but for data tables.
Typing callback parameters
satisfies also provides a contextual type for functions. Parameters in the function expression are inferred from the type on the right.
const increment = ((n) => n + 1) satisfies (value: number) => number;
console.log(increment(1)); // 2The parameter n is number because the contextual type says so. The type of increment is the inferred (n: number) => number.
Combining with as const
as const keeps literals and makes everything readonly. It does not validate anything. Adding satisfies gives you validation on top.
type Settings = {
port: number;
hosts: readonly string[];
};
const settings = {
port: 8080,
hosts: ["a.example", "b.example"],
} as const satisfies Settings;
// typeof settings:
// { readonly port: 8080; readonly hosts: readonly ["a.example", "b.example"] }
console.log(settings.port);The order matters. Write as const satisfies T. The as const produces the readonly literal type, and satisfies checks that type against T.
Gotchas
satisfies is not an assertion
as T tells the compiler to trust you. satisfies T makes the compiler check. An assertion can silently accept an incomplete value:
type Route = { path: string; method: "GET" | "POST" };
const broken = {} as Route; // compiles, but path and method are missing
// const checked = {} satisfies Route; // Error: missing path and method
console.log(broken.path); // undefined at runtime, typed as stringIt does not change the type
Because the inferred type survives, a property typed string | number[] in T can be string in your variable. That is the point. The consequence is that the variable is not assignable to places that need exactly T only if the inferred type is incompatible, which is rare. If you need the variable to have type T (for example, so later reassignments must conform), use an annotation.
It only applies at the expression
satisfies checks one expression at one point. It does not constrain later mutations of the variable. If const config = { ... } satisfies Config has mutable properties, assigning config.mode = "other" is checked against the inferred property type, not against Config.
It is erased at runtime
Like every type feature, satisfies compiles away. Older toolchains that do not know TypeScript 4.9 syntax will fail to parse it, so check your TypeScript, editor, and bundler versions before using it.
Choosing between the options
Use these criteria:
- Use an annotation when the variable should be treated as
Teverywhere afterward, such as a function parameter, a public API, or a value you will reassign. - Use
satisfieswhen the value is a constant table or config and you want both validation and precise per-property types. - Use
as const satisfies Twhen you also want literal and readonly types, for example for lookup tables whose keys and values feed other types. - Use an assertion only when you know something the compiler cannot verify, as covered in the chapter on type assertions.
Exercise
Given type Level = "low" | "high" and the code const limits = { cpu: "low", mem: "high" } satisfies Record<string, Level>;, what is the type of limits.cpu, and what happens if you write limits.disk? How would the answers change if you wrote const limits: Record<string, Level> = { cpu: "low", mem: "high" };?
Show answer
With satisfies, limits.cpu has the literal type "low", and limits.disk is a compile error because the variable keeps its real keys cpu and mem. With the annotation, limits.cpu has type Level ("low" | "high"), and limits.disk compiles, because Record<string, Level> allows any string key. The annotation validates the value but widens the variable's type to the annotation.
Exercise
Why does const s = { tags: ["a", "b"] } as const satisfies { tags: string[] }; fail, and how do you fix it?
Show answer
as const makes tags a readonly ["a", "b"] tuple, and a readonly array is not assignable to the mutable string[]. Change the target type to { tags: readonly string[] }.