Chapter 32: TypeScript Index Signatures

Index Signatures in TypeScript very slowly and clearly, like we’re sitting together with VS Code open, chai on the table, and we’re typing small examples one by one until you really feel comfortable with them.

Index signatures are one of the most misunderstood but most frequently used features when dealing with real-world data — especially API responses, configuration objects, dictionaries, dynamic keys, form data, etc.

1. What is an index signature? (the simplest honest explanation)

An index signature tells TypeScript:

“This object can have any number of properties whose names are strings (or numbers or symbols), and the values of those properties must be of a specific type.”

In other words — it’s how you type objects with dynamic / unknown keys.

Without index signature → TypeScript only allows the properties you explicitly wrote.

With index signature → TypeScript allows extra properties (as long as their values match the declared type).

2. Basic syntax — the most common form

TypeScript
  • [key: string] → the index (property name) must be string
  • : string → the value of any property must be string
TypeScript

3. Real-world examples you see every day (2025–2026)

Example 1: API response with dynamic fields

TypeScript

Example 2: String → number lookup table

TypeScript

Example 3: Form values / uncontrolled inputs

TypeScript

Example 4: CSS-in-JS style objects / theme partials

TypeScript

4. Number index signatures (less common but useful)

TypeScript

Very common when dealing with:

  • HTTP status code → message mappings
  • Array-like objects with numeric keys
  • Sparse arrays / data grids

5. Combining explicit properties + index signature

This is the most realistic pattern — you want some known properties and allow extra ones.

TypeScript

Important warning 2026 — many people now prefer unknown or union types over any in index signatures.

6. Index signatures vs mapped types (quick comparison)

Feature / Goal Index Signature Mapped Type (better in most cases)
Allow any string keys Yes Yes — but usually more controlled
Syntax [key: string]: string { [K in string]: string } or Record<string, string>
Can combine with known properties Yes — but with any risk Yes — and more precise
Preserves literal key types No — all keys widen to string Yes — if using keyof or literals
Modern preference (2025–2026) Used when keys are truly dynamic Preferred when keys are known or union of literals

Modern replacement for loose index signatures — Record utility

TypeScript

7. Common gotchas & modern best practices (2026 edition)

Gotcha 1 — Accessing known keys loses type safety

TypeScript

Gotcha 2 — No literal key safety without mapped types

TypeScript

Modern fix — use Record + as const or mapped types

TypeScript

Best practice 2026 — prefer:

  • Record<string, T> or Record<UnionOfKeys, T> when possible
  • Mapped types with keyof when keys are known
  • Index signatures only when keys are truly dynamic and unknown at compile time

8. Quick cheat-sheet table

Use-case Recommended way 2026 Example syntax
Truly dynamic keys (API, env, forms) Index signature or Record<string, T> [key: string]: unknown
Known but many keys Record<Union | string, T> or as const Record<“success” | “error”, string>
String → anything lookup Record<string, any> (avoid any if possible) Record<string, unknown>
Number keys (status codes, sparse) [index: number]: T [code: number]: string
Extra fields on known object Known properties + index signature (carefully) { id: number; [key: string]: unknown }

Your mini homework (try in playground)

  1. Create type Translations = { [key: string]: string }
  2. Add English, Telugu, Hindi keys
  3. Try accessing translations.french → see it’s allowed but undefined
  4. Change to Record<“en” | “te” | “hi”, string> → see safety improve
  5. Try an index signature with known properties + extra dynamic ones

Which part feels most useful or most confusing for your current code?

Want to go deeper into:

  • Index signatures vs mapped types vs Record<T>
  • Real API response typing with dynamic fields
  • Combining index signatures with generics
  • Common mistakes in form / env / style objects

Just tell me — we’ll zoom right in! 😄

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *