🔷 TypeScript Intermediate

What is the satisfies operator in TypeScript?

Answer

The satisfies operator (TypeScript 4.9) validates that a value matches a type without widening the inferred type of the value. The problem it solves: when you annotate a variable with a type (const colors: Record<string, string | RGB> = { red: "#f00", ... }), TypeScript uses the annotated type everywhere — you lose the specific inferred type of each value. With as assertion, you lose type checking. With satisfies: const colors = { red: "#f00", blue: [0, 0, 255] } satisfies Record<string, string | RGB>; — TypeScript validates the object matches the type (type error if it does not), but still infers the most specific type for each property. colors.red is typed as string (not string | RGB), and colors.blue is typed as number[]. This gives you both type safety (validation) and specificity (narrow inferred type) — the best of both worlds.