Configuration validation
Validate application settings or build metadata while retaining literal and property-specific inference.
TypeScript · Const
The satisfies operator checks that an expression conforms to a target type without changing the expression’s inferred type. It catches structural mistakes while preserving precise property and method inference.
The postfix satisfies operator checks that an expression conforms to a target type, but leaves the expression's inferred type intact. This combines structural validation—such as detecting missing or misspelled properties—with precise property access and method inference afterward.
expression satisfies Type| Name | Type | Description |
|---|---|---|
expression | any expression | The expression whose compatibility is checked. |
Type | type | The type the expression must satisfy. |
The original expression's inferred type, unchanged by the compatibility check.
type RGB = [red: number, green: number, blue: number];
type Colors = "red" | "green" | "blue";
const palette = {
red: [255, 0, 0],
green: "#00ff00",
blue: [0, 0, 255],
} satisfies Record<Colors, string | RGB>;
const greenUpper = palette.green.toUpperCase();
console.log(greenUpper);The object is checked against the required color map while palette.green retains its inferred string type, so string methods remain available.
Validate application settings or build metadata while retaining literal and property-specific inference.
Check that named keys and their values fit a union or mapped record type.
Preserve tuple, array, or string-specific operations after checking a broader contract.
satisfies checks whether an expression is compatible with a type, while preserving the expression's own inferred type. This lets TypeScript report structural mistakes without losing specific property information.
Follow-up: How would you retain tuple information in a validated configuration? Use satisfies with a target containing the tuple type, allowing the original tuple inference to remain available.
A type annotation can make a value conform to a broader type and thereby expose only that broader view. satisfies performs the compatibility check without changing the value's resulting type, so specific members and methods remain usable.
Follow-up: Why can palette.green.toUpperCase() work in the documentation example? The property is inferred as the string literal value type rather than being reduced to the broader string-or-array union.
In a record-like object, satisfies can detect an unknown key such as a misspelled color because every property must be compatible with the target mapping. At the same time, correctly typed properties keep their precise inferred forms.
Follow-up: What happens if a required key is omitted? The expression fails validation because it does not satisfy the required record shape.
Recordimplements@satisfiesTypeScript 5.0