Skip to the solution
MediumFrontend8 min read

How does satisfies validate shape without widening literal types?

266 views

The problem

Question presented to candidate: "You have a route map or theme config checked with satisfies. Which literal types survive and which still widen? Walk me through why, and when you would add as const."

What a strong answer should cover:

  • satisfies T supplies T as the contextual type; a literal is kept when the contextual type at that position contains literal types of the same kind, and widened otherwise
  • So { mode: "dark" } satisfies { mode: "dark" | "light" } keeps "dark", but satisfies { mode: string } or Record<string, unknown> gives string
  • Keys are always kept: keyof typeof routes is "home" | "about", while an annotation with Record<string, string> turns keys into string and accepts typos
  • satisfies Record<Union, V> makes a map exhaustive: a missing or extra key is a compile error
  • as const satisfies T gives validated, deeply readonly literals; the order matters (satisfies T as const is error TS1355); with arrays, check against a readonly array type
  • A tuple target such as [number, number] produces a tuple without as const

Clarifying questions expected:

  • Should the values be readonly constants, or will they be changed later?
  • Do callers need the literal values (for example to derive a union of paths) or only the keys?

Code / implementation expected: Yes β€” the same object checked against a literal-union target and a string target, with Equal checks proving which literals survive, plus an as const satisfies route map.

  • satisfies
  • narrowing

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 Β· 10 sections

Run the code

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

TypeScriptWhich literals survive, keys and exhaustive maps
TypeScriptas const satisfies, order, arrays and tuples

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 56 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 does `allowImportingTsExtensions` work with `moduleResolution: "bundler"` and `.ts` imports?

Continue