Browse topics

TypeScript satisfies

Version
es2025
Since
4.9
Review
Draft

Summary#

The satisfies operator checks an expression against a type while retaining the resulting inferred type of the expression. It is useful for configuration objects whose fields need to stay more specific than the validation type.

Syntax#

ts
const config = expression satisfies TargetType;

Example#

ts
type Policy = { limit: number; delayMs?: number };

const policies = {
  retry: { limit: 3, delayMs: 250 },
  drain: { limit: 10 },
} satisfies Record<"retry" | "drain", Policy>;

console.log(policies.retry.delayMs.toFixed(0), policies.drain.limit);

Expected output#

250 10. The compiler knows that retry.delayMs is present.

Common mistakes#

satisfies does its checking at compile time. Validate external input at runtime. Annotating policies with Record<"retry" | "drain", Policy> would make both delays optional.

Version applicability#

Added in TypeScript 4.9. Compile or transpile this syntax before running it as JavaScript.

Sources#

Attribution
DevLookup contributors
License
CC-BY-4.0