What are Index Signatures in TypeScript?
Understand TypeScript index signatures for dictionary-like objects, valid key types, rules for named properties, and when to prefer Record instead.
Expected Interview Answer
An index signature in TypeScript declares that an object can have any number of properties whose keys share a known type and whose values share a known type, written as { [key: string]: ValueType }.
Index signatures are used when you do not know the exact property names ahead of time, such as a dictionary, lookup map, or cache. The key type must be string, number, or symbol (or a template literal type), and every declared property must be compatible with the value type. They enable flexible, map-like objects while still giving TypeScript enough information to type-check reads and writes.
- Types dictionaries and maps with unknown keys
- Guarantees a consistent value type across all entries
- Allows dynamic property access to be type-checked
- Works alongside known named properties in the same type
- Can be made safer with Record or template literal keys
AI Mentor Explanation
Think of a scoreboard where you do not know every batter's name in advance, but you know each name maps to a run total. An index signature is that rule: any player name you look up is a string key, and whatever you get back is always a number of runs. You cannot predict the keys, yet the value type is guaranteed for every entry.
Step-by-Step Explanation
Step 1
Decide the key type
Choose string, number, symbol, or a template literal type for the property keys.
Step 2
Decide the value type
Pick a single value type every entry must satisfy, e.g. number or a shared interface.
Step 3
Write the signature
Declare it as { [key: string]: ValueType } inside an interface or type alias.
Step 4
Mix in known keys carefully
Any explicit named properties must be assignable to the index value type.
Step 5
Prefer Record when fixed
Use Record<K, V> for a known finite key set instead of a broad index signature.
What Interviewer Expects
- Correct syntax { [key: string]: ValueType }
- Knowing valid key types: string, number, symbol, template literals
- Awareness that named properties must match the index value type
- When to use an index signature versus Record or a Map
- Understanding the safety trade-offs of dynamic access
Common Mistakes
- Adding a named property whose type is incompatible with the index value type
- Using an index signature when a Map or Record would be safer
- Assuming a read always returns a defined value (it may be undefined at runtime)
- Mixing string and number index signatures incorrectly
- Forgetting that overly broad signatures weaken type safety
Best Answer (HR Friendly)
“An index signature lets you describe an object whose property names you do not know ahead of time, like a dictionary, while still guaranteeing what type the values are. It is how TypeScript type-checks flexible, map-like objects.”
Code Example
interface ScoreBoard {
// any player name maps to a numeric score
[player: string]: number;
}
const scores: ScoreBoard = {};
scores['Kohli'] = 82;
scores['Root'] = 44;
function total(board: ScoreBoard): number {
return Object.values(board).reduce((sum, runs) => sum + runs, 0);
}
// Prefer Record for a known, fixed key set:
type Roles = Record<'admin' | 'editor' | 'viewer', boolean>;Follow-up Questions
- What key types are allowed in an index signature?
- How does Record<K, V> differ from an index signature?
- Why might a lookup be typed as defined but be undefined at runtime?
- How do named properties interact with an index signature?
- When would a Map be a better choice than an indexed object?
MCQ Practice
1. Which is valid syntax for a string-keyed index signature?
The correct form places the key name and its type in brackets: { [key: string]: number }.
2. A named property in a type with an index signature must be...
Every explicit property must be compatible with the declared index value type.
3. For a known, finite set of keys, what is usually preferred?
Record<K, V> expresses a fixed key set precisely and is safer than an open index signature.
Flash Cards
What is an index signature? — A declaration that an object has arbitrary keys of a known type mapping to values of a known type.
Valid index signature key types? — string, number, symbol, and template literal types.
Rule for named properties with an index signature? — They must be assignable to the index signature's value type.
Index signature vs Record? — Use Record<K, V> for a known finite key set; index signatures for open-ended keys.