Sub topic 4
Meet `satisfies`: Check Without Widening
This chapter assumes that you know basic TypeScript types, unions, object literals, type inference, and Record. You should also know that TypeScript checks types at compile time, before JavaScript runs.
The satisfies operator was added in TypeScript 4.9. It checks whether an expression is compatible with a type, but it does not replace the expression's inferred type with that target type.
The basic syntax
Write satisfies after an expression:
const value = expression satisfies SomeType;TypeScript performs two jobs:
- It checks that
expressioncan be used asSomeType. - It keeps the type inferred for
expression.
This differs from a type annotation:
const value: SomeType = expression;A type annotation gives the variable the declared type. satisfies validates the expression while preserving useful details about its actual structure.
| Syntax | Checks compatibility | Keeps the expression's inferred type |
|---|---|---|
| `const x: T = value` | Yes | Usually no |
| `const x = value as T` | No reliable compatibility check | No, it uses `T` |
| `const x = value satisfies T` | Yes | Yes |
A type annotation can lose precision
Suppose a color palette allows either a hexadecimal string or an RGB tuple:
type Color = "red" | "green" | "blue";
type RGB = [number, number, number];
const annotated: Record<Color, string | RGB> = {
red: [255, 0, 0],
green: "#00ff00",
blue: [0, 0, 255],
};
console.log(annotated.green.toUpperCase());
// Error: green might be an RGB tuple.The declared type of annotated.green is string | RGB. Even though the object contains a string for green, the annotation tells TypeScript only that the property could contain either allowed value.
You can narrow the union before using the value:
if (typeof annotated.green === "string") {
console.log(annotated.green.toUpperCase());
}The annotation is useful when you want the variable to expose exactly one general type. However, it can hide facts that TypeScript could otherwise infer from the object.
`satisfies` validates and preserves
Replace the annotation with satisfies:
type Color = "red" | "green" | "blue";
type RGB = [number, number, number];
const palette = {
red: [255, 0, 0],
green: "#00ff00",
blue: [0, 0, 255],
} satisfies Record<Color, string | RGB>;
console.log(palette.green.toUpperCase());
console.log(palette.red.map((number) => number * 2));The object must satisfy Record<Color, string | RGB>. Therefore:
red,green, andbluemust be present.- Every value must be a string or an RGB tuple.
palette.greenremains known as a string.palette.redremains known as an RGB tuple.
The target type acts as a constraint. It does not become the complete type of palette.
Contextual typing still applies
A type used with satisfies can provide contextual typing. Contextual typing means that TypeScript uses the expected type to help infer an expression while checking it.
In the palette example, the target type tells TypeScript that array values can be RGB tuples. This lets TypeScript check the tuple shape:
type RGB = [number, number, number];
const primary = {
red: [255, 0, 0],
} satisfies Record<string, string | RGB>;
console.log(primary.red[0] * 2);The value for red must have exactly three numeric elements because it is checked against RGB.
Contextual typing does not mean that every value becomes the target type. The resulting type still reflects the expression itself. A property that is written as a string remains usable as a string.
Checking object literals
satisfies is especially useful for configuration objects. It catches misspelled property names and invalid values while keeping precise property types.
type ServerOptions = {
port: number;
host?: string;
};
const options = {
port: 8080,
host: "localhost",
} satisfies ServerOptions;
console.log(options.port.toFixed(0));
console.log(options.host.toUpperCase());The object is checked against ServerOptions. If you write hots instead of host, TypeScript reports an excess property error:
const broken = {
port: 8080,
hots: "localhost",
} satisfies ServerOptions;The property hots is not part of ServerOptions, so the error points to the typo. This is safer than using an assertion such as as ServerOptions, which can hide the extra property.
Use `satisfies` when the expression matters
Choose satisfies when you need both validation and access to the expression's specific properties or value types.
For example, this object must use numeric values, but each property remains available through its own key:
type Limits = Record<"retries" | "timeout", number>;
const limits = {
retries: 3,
timeout: 5000,
} satisfies Limits;
console.log(limits.retries + 1);Use a type annotation when you intentionally want a variable to be viewed only through a general interface. Use as only when you have stronger knowledge than TypeScript and have verified that the assertion is true. Use satisfies when TypeScript should check your claim without discarding details from the expression.
Exercise
What is the main difference between const item: Config = value and const item = value satisfies Config?
Show answer
Both check compatibility with Config, but the annotation gives item the type Config, while satisfies keeps the type inferred from value.