TypeScript Developer interview question · Most asked
What does the `satisfies` operator do and how does it differ from a type annotation?
Summary
satisfies (TypeScript 4.9) checks that an expression conforms to a type without changing the expression's inferred type, so you get validation and keep precise literal types. An annotation (const x: T = ...) widens the variable to T.
Longer Answer
Typical use: configuration objects where you want to ensure all keys are valid and values have the right shape, but still want autocomplete on the specific keys and literal values.
Code Example
type Route = { path: string; auth: boolean };
const routes = {
home: { path: "/", auth: false },
admin: { path: "/admin", auth: true },
} satisfies Record<string, Route>;
routes.admin.path; // OK, and key "admin" is known
// routes.missing; // Error: property doesn't exist
const annotated: Record<string, Route> = routes;
annotated.anything; // no error: keys widened to string
Key Points
- Validates without widening
- Keeps literal/specific inferred types
- Great for config and lookup objects
- Annotations widen to the declared type
More most-asked TypeScript Developer questions
- Explain `any`, `unknown` and `never`.
- What are the differences between `interface` and `type` aliases?
- What is structural typing in TypeScript?
- What is type narrowing and what techniques does TypeScript support?
- What are discriminated unions and why are they powerful?
- What are generics and how do constraints work?
- Which built-in utility types do you use most, and what do they do?
- What is TypeScript and what problems does it solve?