TypeScript Master (50 Qs)•Part 2: Advanced Type System & Metaprogramming (Mid-Level)
What is the `satisfies` operator in TypeScript 4.9+?
junior LevelCode ExampleTypeScript
In Simple Words:
Before satisfies, you had a dilemma:
- If you annotated
const palette: Colors = { ... }, you validated the shape, but lost the specific inferred property types (property values widened tostring). - If you omitted the annotation
const palette = { ... }, you kept specific types, but had zero validation of required keys.
The satisfies operator validates that an expression matches a type without changing or widening the inferred type!
typescript
type Colors = Record<string, string | number[]>;
// Validated against Colors, BUT keeps exact RGB tuple type!
const theme = {
primary: "#10b981",
secondary: [255, 0, 0],
} satisfies Colors;
// Keeps specific methods:
theme.primary.toLowerCase(); // Perfectly safe! TS knows it's a string, not string | number[]