0Pricing
TypeScript Academy · Lesson

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]) // ana

All lessons in this course

  1. Defining Index Signatures
  2. String vs Number Index Signatures
  3. Combining Known and Dynamic Keys
  4. Index Signatures vs Record
← Back to TypeScript Academy