Index Signatures vs Record
Choose between index signatures and the Record utility type.
Two Ways to Type a Map
TypeScript offers two common ways to describe a dictionary: an index signature like { [k: string]: V }, and the built-in utility type Record<K, V>. They overlap heavily but read differently.
The Record Utility Type
Record<K, V> constructs an object type whose keys are K and whose values are V. With string as the key, it is equivalent to a string index signature.
type Scores = Record<string, number>
const game: Scores = { alice: 10, bob: 7 }
console.log(game.alice)All lessons in this course
- Defining Index Signatures
- String vs Number Index Signatures
- Combining Known and Dynamic Keys
- Index Signatures vs Record