String vs Number Index Signatures
Understand the rules and quirks of numeric keys.
Two Kinds of Index Keys
TypeScript supports both string and number index signatures. They look similar but behave differently because of how JavaScript actually stores object keys.
A Number Index Signature
A number index signature { [i: number]: T } describes objects indexed by numeric position — most naturally, arrays and array-like structures.
type NumberList = { [index: number]: string }
const names: NumberList = { 0: "ana", 1: "leo" }
console.log(names[0]) // anaAll lessons in this course
- Defining Index Signatures
- String vs Number Index Signatures
- Combining Known and Dynamic Keys
- Index Signatures vs Record