Objects & Interfaces
Index Signatures
Master index signatures in TypeScript, dictionary object patterns, template literal index signatures, Record utility type, and strict index checking.
Sometimes you do not know the exact property names of an object in advance, but you know the types of the keys and the types of the values. In TypeScript, Index Signatures allow you to define dictionary-like objects with dynamic keys.
1. Syntax for Index Signatures
An index signature uses square brackets [key: KeyType]: ValueType inside an object type or interface:
2. Supported Key Types
Index signature keys in TypeScript must be of type string, number, symbol, or Template Literal Types.
3. Mixing Explicit Properties with Index Signatures
If an interface declares both explicit properties AND an index signature, all explicit property values MUST be assignable to the index signature value type.
4. Alternative: The Record Utility Type
Instead of writing explicit index signatures [key: string]: V, modern TypeScript codebases frequently use the built-in Record utility type:
5. Modern Feature: Template Literal Index Signatures
TypeScript allows restricting index signature keys using Template Literal types:
6. Interactive Code Playground
Test index signatures below:
7. Common Pitfalls & Edge Cases
undefined at Runtime: Accessing an un-set key on Record returns undefined at runtime, even though TypeScript infers the type as T unless noUncheckedIndexedAccess: true is enabled in tsconfig.json.8. Summary Comparison Table
| Feature | Index Signature Syntax | Record Syntax |
|---|---|---|
| Basic Usage | { [key: string]: ValueType } | Record |
| Supported Keys | string, number, symbol, Template Literals | String literal unions, string, number |
id: number; [key: string]: any) | Must use Intersection ({ id: number } & Record) |