Interview Q&A / TypeScript

IntermediateQuick answer1 min readUpdated 3 Oct 2026

How does Record with a union of keys help catch missing cases?

SHORT ANSWER

Record<Union, V> requires an entry for every member of the union. Adding a new member later makes every object typed with it fail until the new key is added, so you cannot forget a case.

Explain it simply

A fare chart at a railway counter should list every class: Sleeper, 3AC, 2AC. If a new class is introduced, the chart must be updated or it is incomplete.

Record<Class, number> is that rule enforced by the compiler. The keys are exactly the union members, no more and no less. Compare this to Record<string, number>, which accepts any key and never complains about a missing one.

Example

ts
type Coach = "SL" | "3A" | "2A";

const fare: Record<Coach, number> = {
  SL: 450,
  "3A": 1200,
  "2A": 1700,
};
console.log(fare["3A"]);            // Output: 1200

// Missing a key is an error:
// const bad: Record<Coach, number> = { SL: 450, "3A": 1200 };
// Error: Property '2A' is missing

// Extra key is an error too (in an object literal):
// const bad2: Record<Coach, number> = { SL: 1, "3A": 2, "2A": 3, "1A": 4 };

// Loose version: any string key, nothing is enforced
const loose: Record<string, number> = { anything: 1 };

What to say in the interview

"Record with a union as the key type forces an entry per member, so adding a new union member breaks the build until every lookup table is updated. With Record<string, V> you get no such guarantee, so I prefer a union when the key set is known."

Common mistakes

  • Using Record<string, X> for a fixed set of keys and losing the missing-key check.

#utility-types#record#exhaustiveness

Related questions

Practise more TypeScript questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open TypeScript quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.