What is the satisfies operator in TypeScript?
Why Interviewers Ask This
This question targets practical, hands-on experience with TypeScript. Interviewers want to see if you've worked with these concepts in real projects, not just read about them. Strong answers include concrete examples.
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.
Pro Tip
Demonstrate both theoretical understanding and practical experience. Say what it is, then give an example of how you actually used it in a TypeScript codebase.
Previous
What is the difference between import type and import in TypeScript?
Next
What are discriminated unions in TypeScript?