Intermediate · 7 of 16 · 10 min read
The satisfies operator
Check a value against a type without losing what the compiler inferred — the operator that makes most uses of as on a literal unnecessary.
- satisfies
Before TypeScript 4.9 you had to choose: annotate a value and lose the detail the compiler
inferred, or assert it and lose the check. satisfies is both — conformance verified, inferred
type kept.
The three-way comparison
type Config = Record<string, string | number>;
const annotated: Config = { host: "localhost", port: 8080 };
const asserted = { host: "localhost", port: 8080 } as Config;
const satisfied = { host: "localhost", port: 8080 } satisfies Config;
console.log(satisfied.port.toFixed(0)); // "8080" — port is number
// console.log(annotated.port.toFixed(0));
// Property 'toFixed' does not exist on type 'string | number'
// console.log(asserted.port.toFixed(0)); // the same problem
The annotation widened every value to string | number, so the detail that port is a number is
gone. satisfies kept it — and still checked that every value conformed.
| Annotation | as |
satisfies |
|
|---|---|---|---|
| Conformance checked | yes | no | yes |
| Excess properties caught | yes | no | yes |
| Missing properties caught | yes | no | yes |
| Inferred type kept | no | no | yes |
| Emits anything | no | no | no |
Catching the typo
This is the practical reason it matters. People reach for as to attach a type to a
configuration object, and in doing so switch the checking off:
type Palette = { primary: string; secondary: string };
const a = { primary: "#fff", secondry: "#000" } as Palette;
// compiles — `secondary` is missing and `secondry` is a typo, and nothing says so
// const b = { primary: "#fff", secondry: "#000" } satisfies Palette;
// Object literal may only specify known properties, and 'secondry' does not exist
// Property 'secondary' is missing
const c = { primary: "#fff", secondary: "#000" } satisfies Palette;
console.log(a.primary, c.secondary);
So the rule is short: anywhere you were about to write as on an object literal, you wanted
satisfies.
Keeping the keys
The other common win. Annotating with Record<string, …> loses which keys exist:
type Routes = Record<string, { path: string }>;
const annotated: Routes = {
home: { path: "/" },
about: { path: "/about" },
};
const satisfied = {
home: { path: "/" },
about: { path: "/about" },
} satisfies Routes;
console.log(satisfied.home.path); // "/"
// console.log(annotated.hoem.path); // no error — any string key is allowed
// console.log(satisfied.hoem.path); // Property 'hoem' does not exist
Which means the keys can be derived into a union:
const routes = {
home: { path: "/" },
about: { path: "/about" },
} satisfies Record<string, { path: string }>;
type RouteName = keyof typeof routes; // "home" | "about"
function go(name: RouteName) { return routes[name].path; }
console.log(go("about")); // "/about"
// go("contact"); // not assignable
One object, checked against a contract, with the key union falling out of it. That is the pattern
satisfies was added for.
With as const
They compose, and they do different jobs: as const makes the inference maximally narrow,
satisfies checks it against a contract.
type Theme = { colours: Record<string, string>; spacing: number[] };
const theme = {
colours: { bg: "#000", fg: "#fff" },
spacing: [4, 8, 16],
} as const satisfies Theme;
type Colour = keyof typeof theme.colours; // "bg" | "fg"
const c: Colour = "bg";
console.log(theme.colours[c], theme.spacing[0]); // "#000" 4
// theme.spacing.push(32); // readonly, thanks to as const
Order matters: as const satisfies T, not the other way round.
Where an annotation is still right
satisfies is not a replacement for annotations everywhere. Annotate when you want the wider
type:
interface Plugin { name: string; run(): void }
// The variable should be reassignable to any Plugin, so annotate:
let current: Plugin = { name: "a", run: () => {} };
current = { name: "b", run: () => {} };
// And a function parameter or return is an annotation by definition:
function load(plugin: Plugin): Plugin { return plugin; }
console.log(load(current).name);
A rough division:
- Function parameters and returns → annotation. There is no literal to preserve.
- A
letthat will be reassigned → annotation, so the wider type is the declared one. - A
constobject literal you will read keys or values off →satisfies. - A value whose literal types you need →
as const, withsatisfiesif there is a contract.
It checks, it does not change
A detail worth being precise about: satisfies produces the same value and the same inferred
type as writing nothing at all. It only adds a check.
const a = { x: 1 };
const b = { x: 1 } satisfies { x: number };
// a and b have exactly the same type: { x: number }
const c: typeof a = b;
console.log(c.x);
Which is why it can be added to existing code with no risk: either it compiles, or it has found something.
Common mistakes
Expecting it to widen
Mistake. const config = { env: "dev" } satisfies { env: string } and then assigning
"prod" to it.
Why it fails. The inferred type is kept, and for a const object property that is string
here — but with as const it would be "dev", and the assignment would fail.
What to do instead. Annotate when you want the wider type; satisfies is for checking.
Using it on a function parameter
Mistake. function f(options satisfies Options).
Why it fails. It is an operator on an expression, not an annotation — a parameter has no
inferred type to preserve.
What to do instead. function f(options: Options).
Reaching for as out of habit
Mistake. as SomeType on a literal, because that is what the codebase does.
Why it fails. It silences the excess property check and the missing property check, which are
the two things that catch real mistakes in configuration objects.
What to do instead. satisfies. It is a drop-in in exactly that position.
Check yourself
2 questions from the bank on what you just read. Answering here does not affect your quiz history.
What does satisfies give you that an annotation does not?
The difference between the three forms shows up in what the compiler still knows afterwards.
Which of these reports the typo in colour?
type Palette = { primary: string; secondary: string };
const a = { primary: "#fff", secondry: "#000" } as Palette;
const b = { primary: "#fff", secondry: "#000" } satisfies Palette;Further reading
Everything above is meant to be enough on its own. These are for when you want the full specification of a detail.
Reaching the end of a lecture marks it as read.