Skip to the solution
MediumFrontend8 min read

What does the satisfies operator do?

216 views

The problem

Question presented to candidate: "What does the satisfies operator do in TypeScript? How is const x = value satisfies T different from const x: T = value and from value as T?"

What a strong answer should cover:

  • expr satisfies T checks that the expression is assignable to T (including excess-property and missing-property checks) but the expression keeps its own inferred type
  • Compared with an annotation: both check, but the annotation replaces the type with T, losing per-property detail (a string | RGB value stays a union)
  • Compared with as: an assertion barely checks (a missing key slips through), satisfies reports the error at the offending property
  • It provides a contextual type, so callback parameters inside the object are typed and tuple targets produce tuples
  • It is an expression operator: usable in returns, arguments and exports, not only on variable declarations; it is erased from the JavaScript
  • Introduced in TypeScript 4.9 (November 2022)

Code / implementation expected: Yes β€” the same object written with an annotation, with as, and with satisfies, showing one compile error each would or would not catch.

  • satisfies

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

8 min read Β· 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

TypeScriptAnnotation vs as vs satisfies on a colour palette
TypeScriptContextual typing and satisfies as an expression

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 58 of 81 in TypeScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

How do you get a union of an object's values as a type?

Continue