Sub topic 8
Validating Shapes with `as const satisfies`
This chapter assumes you know basic TypeScript types, literal types, union types, arrays, object types, typeof, and keyof. You should also know the difference between a type annotation and a type assertion.
The combination as const satisfies solves two problems at once:
as constpreserves exact literal types and makes object properties or arrays readonly.satisfieschecks that a value matches a required shape without replacing the value's inferred type.
This is useful for configuration objects, route tables, command registries, and other values that act as both runtime data and a source for compile-time types.
The problem with annotations
Suppose an application stores its routes in an array:
type Route = {
path: string;
method: "GET" | "POST";
};
const routes: Route[] = [
{ path: "/users", method: "GET" },
{ path: "/users", method: "POST" },
];
const firstPath = routes[0].path;
// stringThe annotation checks that every array item has the required fields. However, it also tells TypeScript to use the declared type Route[]. The specific path "/users" is widened to string.
That may be too broad when you want to derive a type from the data:
type Path = (typeof routes)[number]["path"];
// stringThe Path type is only string, even though the actual table currently contains one path.
Removing the annotation preserves more information, but it loses shape validation:
const uncheckedRoutes = [
{ path: "/users", method: "GET" },
{ path: "/users", methd: "POST" },
];
// The misspelled "methd" property is not rejected here.The satisfies operator provides validation without forcing the expression to use the target type as its final inferred type.
Combining `as const` and `satisfies`
Write the combination like this:
type Route = {
path: string;
method: "GET" | "POST";
};
const routes = [
{ path: "/users", method: "GET" },
{ path: "/users", method: "POST" },
] as const satisfies readonly Route[];
type Method = (typeof routes)[number]["method"];
type Path = (typeof routes)[number]["path"];
console.log(routes[0].path);The expression has two effects:
as constpreserves the literal values and makes the array and its objects readonly.satisfies readonly Route[]checks that every item has apathstring and an allowedmethod.
The resulting types are narrow:
// typeof routes:
// readonly [
// { readonly path: "/users"; readonly method: "GET" },
// { readonly path: "/users"; readonly method: "POST" }
// ]
// Method:
// "GET" | "POST"
// Path:
// "/users"The readonly before Route[] describes the value produced by as const. A readonly tuple can be checked against a readonly array type because the target type does not promise that it will mutate the array.
`satisfies` catches mistakes
A shape check is useful because it catches misspelled properties and invalid values at the declaration site:
type Route = {
path: string;
method: "GET" | "POST";
};
const routes = [
{ path: "/users", method: "GET" },
{ path: "/users", methd: "POST" },
] as const satisfies readonly Route[];TypeScript reports an error for methd. The object does not satisfy Route because it has no method property.
It also rejects invalid literal values:
const routes = [
{ path: "/users", method: "GET" },
{ path: "/users", method: "DELETE" },
] as const satisfies readonly Route[];"DELETE" is not part of the allowed method union.
Without satisfies, TypeScript might infer a value that is internally consistent but does not meet the contract expected by the rest of the program.
Object registries
The same pattern works with named object properties. Suppose each command needs a label and a handler:
type Command = {
label: string;
run: () => string;
};
const commands = {
start: {
label: "Start the service",
run: () => "started",
},
stop: {
label: "Stop the service",
run: () => "stopped",
},
} as const satisfies Record<string, Command>;
type CommandName = keyof typeof commands;
// "start" | "stop"
const name: CommandName = "start";
console.log(commands[name].run());Record<string, Command> means that every property value must satisfy Command. The key names are still inferred from the object, so keyof typeof commands produces "start" | "stop" rather than plain string.
This gives the runtime object and the related type a single source of truth.
Order matters
Use as const satisfies, not the reverse:
const settings = {
mode: "dark",
retries: 3,
} as const satisfies {
mode: "dark" | "light";
retries: number;
};The const assertion applies to the literal expression. The satisfies check then validates the resulting readonly, literal-preserving value.
A type assertion such as as SomeType has a different purpose:
const settings = {
mode: "dark",
retries: 3,
} as {
mode: "dark" | "light";
retries: number;
};This tells TypeScript to treat the value as the asserted type. It can hide useful literal information, and assertions can sometimes silence mistakes that a direct check would reveal. Prefer satisfies when the goal is validation.
The following table summarizes the common choices:
| Syntax | Checks the shape | Preserves literal types | Makes values readonly |
|---|---|---|---|
| Type annotation | Yes | Usually no | No |
| `as const` | No | Yes | Yes |
| `satisfies` | Yes | Usually yes | No |
| `as const satisfies T` | Yes | Yes | Yes |
A practical decision process
Use as const satisfies when all of these are true:
- The value is a literal object or array.
- You want exact values such as
"/users"or"GET". - You want TypeScript to check a required structure.
- The value should be treated as readonly.
Use only satisfies when the value should remain mutable but you still want shape validation. Use a normal type annotation when you intentionally want the broader declared type and do not need to derive exact unions from the value.
The combination does not perform runtime validation. Data loaded from JSON, a request, or a database still needs runtime checks. satisfies only helps TypeScript check the source code during compilation.
Exercise
Write a declaration for a levels array containing "info" and "error". It must be checked as a readonly array of objects with a name property, and LevelName must become the union "info" | "error".
Show answer
type Level = { name: string }; const levels = [{ name: "info" }, { name: "error" }] as const satisfies readonly Level[]; type LevelName = (typeof levels)[number]["name"];