Sub topic 12
Validating Constants with `satisfies`
This chapter assumes that you know basic TypeScript types, object types, functions, and object literals. It also assumes that you have used as const and understand that TypeScript can infer a type from a value.
The satisfies operator checks whether a value matches a required type without changing the type TypeScript infers for that value. It is useful for configuration objects, lookup tables, and other constants where you want both validation and precise property types.
The problem with type annotations
Suppose a palette must contain either strings or arrays of numbers:
const palette: Record<"red" | "green", string | number[]> = {
red: [255, 0, 0],
green: "#00ff00",
};The annotation validates the object. It confirms that red and green exist and that each value is a string or an array of numbers.
However, the annotation also describes palette.red as the broad union string | number[]. TypeScript no longer knows that palette.red is specifically an array. This makes array methods less convenient:
palette.red.map((value) => value * 2);
// Error: Property 'map' does not exist on type 'string | number[]'.You can use a type assertion to silence errors, but an assertion tells TypeScript to trust you. It does not verify that the value really matches the type.
Checking without widening
Use satisfies when you want TypeScript to check the shape while preserving the value's inferred type:
const palette = {
red: [255, 0, 0],
green: "#00ff00",
} satisfies Record<"red" | "green", string | number[]>;
const doubled = palette.red.map((value) => value * 2);
console.log(doubled); // [510, 0, 0]
const upperGreen = palette.green.toUpperCase();
console.log(upperGreen); // "#00FF00"The Record<K, V> type describes an object with keys K and values V. Here, the object must contain exactly the required color keys, and each value must be a string or number array.
The check happens at compile time. At runtime, satisfies produces no JavaScript and does not validate data received from a user or a network.
Detecting missing and incorrect entries
The expected type can catch misspelled keys, missing keys, and invalid values:
type Route = {
method: "GET" | "POST";
requiresAuth: boolean;
};
const routes = {
home: { method: "GET", requiresAuth: false },
createUser: { method: "POST", requiresAuth: true },
// creatUser: { method: "POST", requiresAuth: true }, // Misspelled key
} satisfies Record<"home" | "createUser", Route>;If createUser is omitted, TypeScript reports that a required property is missing. If creatUser is used instead, TypeScript reports an unknown property. If method is set to "PUT", TypeScript reports that "PUT" is not one of the permitted literal values.
The object still keeps useful specific information. For example, routes.home.method remains the literal type "GET" rather than becoming the broader "GET" | "POST".
`satisfies` with `as const`
satisfies does not automatically make an object immutable or preserve every literal value. Add as const when you also want readonly properties and readonly literal values:
const statuses = {
active: { label: "Active", color: "green" },
inactive: { label: "Inactive", color: "gray" },
} as const satisfies Record<
"active" | "inactive",
{ label: string; color: string }
>;
console.log(statuses.active.label); // "Active"
// statuses.active.label = "Enabled"; // Error: readonly propertyThe order is important. as const satisfies SomeType first keeps the value deeply literal and readonly, then checks that it matches SomeType.
How the pieces fit
Use satisfies when the expected type expresses a contract and the individual properties need to remain precise. Use a type annotation when you intentionally want the value viewed through a broader type.
Exercise
What is the main difference between const table: Table = value and const table = value satisfies Table?
Show answer
Both check that the value matches Table, but satisfies preserves the value's more specific inferred property types instead of replacing them with the broader Table view.