TypeScript's satisfies, and when to reach for it
1 min read
On this page
satisfies has been in TypeScript since 4.9, and it solves a problem you've probably hit without
naming it: you want a value checked against a type, but you don't want the type to replace what
TypeScript already knows about the value.
The problem with annotations
Annotating a variable checks it, then widens it to the annotation.
type Route = { path: string; label: string };
const nav: Record<string, Route> = {
home: { path: "/", label: "Home" },
blog: { path: "/blog", label: "Blog" },
};
nav.blog.path; // fine
nav.contact.path; // also "fine": compiles, then crashes at runtimeBecause the type is now Record<string, Route>, TypeScript has forgotten which keys exist.
satisfies checks without widening
const nav = {
home: { path: "/", label: "Home" },
blog: { path: "/blog", label: "Blog" },
} satisfies Record<string, Route>;
nav.contact.path; // error: Property 'contact' does not existThe value is still checked: a missing label or a typo in a key is an error. But the inferred type
is kept, so nav knows exactly which keys it has.
Pair it with as const
Add as const and the values keep their literal types too:
const theme = {
accent: "amber",
radius: 8,
} as const satisfies Record<string, string | number>;
type Accent = typeof theme.accent; // "amber", not stringWhere I use it
- Config objects: navigation, design tokens, feature flags.
- Lookup tables keyed by a union, so a missing key is a compile error.
- Storybook stories:
satisfies Meta<typeof Button>keeps the args typed.
- TypeScript
