Codectionary / Developer documentation / TypeScript

Index Signatures

An index signature lets an interface or type describe an object with dynamic keys of a known type, when you don't know the exact property names in advance - only their pattern. This is common for dictionary-like objects, such as a lookup table keyed by user ID or product SKU.

Syntax

interface Name {
  [key: string]: ValueType;
}

Examples

A Basic Index Signature

Describing an object where keys are unknown but values share a common type.

interface StringDictionary {
  [key: string]: string;
}

const translations: StringDictionary = {
  hello: "Hola",
  goodbye: "Adiós"
  // any number of string keys are allowed, all values must be strings
};

console.log(translations.hello);    // "Hola"
console.log(translations["goodbye"]); // "Adiós"

Combining Known and Dynamic Properties

An interface can mix specific required properties with an index signature for the rest.

interface Config {
  name: string;        // a specific, required property
  [key: string]: string | number; // plus any number of other string or number properties
}

const settings: Config = {
  name: "MyApp",
  version: 2,
  environment: "production"
};

Best practices

  • Use index signatures for genuinely dynamic-key data, like a dictionary or lookup table - not as a shortcut to avoid defining specific properties
  • Consider Record<KeyType, ValueType> as a more concise alternative to an index signature for simple dictionary types
  • Remember all specifically named properties must be compatible with the index signature's value type when both are combined
  • Prefer a Map over an index-signature object when keys are added/removed frequently, or when you need reliable iteration and size tracking

At a glance

Purpose
Static types for JavaScript
File extension
.ts · .tsx
Runs in
Compiled to JavaScript
Usually used with
JavaScript and its ecosystem

Specifications & further reading

Related TypeScript documentation