Skip to the solution
MediumFrontend8 min read

What is the Record utility type?

373 views

The problem

Question presented to candidate: "What does Record<K, V> do in TypeScript? When would you reach for it instead of writing an index signature or an object type by hand?"

What a strong answer should cover:

  • Record<K, V> builds an object type whose keys are K and whose values are all V; in lib.es5.d.ts it is the mapped type { [P in K]: T } with K extends keyof any
  • With a union of literal keys every key is required and unknown keys are rejected, which gives exhaustiveness: adding a union member breaks every table that misses it
  • With string keys it is exactly an index signature, with the same "missing key is still typed V" gotcha
  • Common patterns: lookup tables of labels or handlers, Partial<Record<K, V>> for optional keys, grouping results with Record<string, T[]>
  • Limits: Object.keys still returns string[], and plain objects carry prototype keys; Map is better for dynamic, user-supplied keys

Code / implementation expected: Yes β€” a lookup table keyed by a status union, and what tsc says when a key is missing.

  • utility-types
  • record

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.

TypeScriptRecord as a mapped type and an exhaustive lookup table
TypeScriptRecord<string, V> vs Record<Union, V>, grouping and Partial

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 49 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

What are function overloads?

Continue