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

← All TypeScript Developer questions