Advertisement
Beginner TypeScript, Index Signatures & Record Types

What is an index signature and what problem does it solve?

Short answer

An index signature describes the type of ALL properties an object can have when the exact set of key names isn't known ahead of time, using the syntax `{ [key: string]: ValueType }` — it lets you type dictionary-like objects with dynamic keys.

Save TypeScript mock interview
Advertisement
AModel answer
An index signature describes the type of ALL properties an object can have when the exact set of key names isn't known ahead of time, using the syntax `{ [key: string]: ValueType }` — it lets you type dictionary-like objects with dynamic keys.
interface Scores {
  [studentName: string]: number;
}
const scores: Scores = { alice: 90, bob: 85 };
scores.carol = 78; // allowed, matches the index signature
Real-world example
Typing a lookup object where keys come from user input or external data and aren't known in advance, like a scores-by-name map.
Likely follow-ups
  • Can an index signature's key type be something other than string or number?
Related topics