Course contentsShow
← TypeScript
Lesson 317 of 1,7967. ObjectsFree lesson

Index Signatures and Record Utility

How Record<Keys, Value> provides a cleaner alternative to index signatures for specific sets of allowed keys.

Index Signatures and Record Utility

What you'll learn: How Record<Keys, Value> gives you a cleaner, more type-safe way to define objects with specific allowed keys compared to traditional index signatures.

The Limitation of Index Signatures

Index signatures are powerful, but they're too open-ended when you know exactly which keys should exist. An index signature like [key: string]: number accepts any string key, even ones you didn't intend:

interface Scores {
  [subject: string]: number;
}

const grades: Scores = {
  math: 95,
  science: 88,
  typooooo: 100  // Oops! No error caught
};

Enter the Record Utility

Record<Keys, Value> lets you specify exactly which keys are allowed while keeping all their values the same type:

type Subject = "math" | "science" | "history";
type Grades = Record<Subject, number>;

const grades: Grades = {
  math: 95,
  science: 88,
  history: 92
};

// grades.typooooo = 100;  // Error! Property doesn't exist

Think of it like this: an index signature is a hotel that accepts anyone with any name. A Record is a VIP list — only specific, pre-approved names get in.

Why Record Is Better for Known Keys

  • Autocomplete works: Your editor suggests only valid keys
  • Typos are caught: Invalid keys produce errors immediately
  • Intent is clear: Readers see exactly which keys matter
  • Type safety: You can't accidentally add unexpected properties

Use index signatures when keys are truly dynamic and unknowable. Use Record when you know the exact set of keys upfront.

Key Takeaway: Record<Keys, Value> provides stronger type safety than index signatures when you know your allowed keys in advance, turning runtime surprises into compile-time errors.