Skip to main content

Lesson 6 of 8

Index Signatures & Record Types

Index signatures define types for dynamic property access - when you don't know all property names ahead of time. `Record<K, V>` is the utility type equivalent. Together they type dictionaries, maps, lookup tables, and config objects with dynamic keys.

10 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Index Signatures & Record - Typed Dictionaries

Index Signatures & Record - Typed Dictionaries
// ── Index signature ───────────────────────────────────────────
interface StringMap {
  [key: string]: string;
}
const headers: StringMap = {
  "Content-Type":  "application/json",
  "Authorization": "Bearer token123",
};
headers["X-Custom"] = "value";    // ✅ dynamic key assignment

// ── Index signature with known + dynamic properties ───────────
interface TaskData {
  readonly id: number;          // known mandatory field
  title: string;                // known mandatory field
  [key: string]: unknown;       // additional dynamic fields
}
// Note: explicit properties must be assignable to the index type.
// id: number is NOT assignable to string, so we use unknown.

// ── Record<K, V> - preferred for clean dictionary types ───────
// Record<K, V> = { [P in K]: V }

// Fixed key set (union literal keys):
type StatusLabel = Record<TaskStatus, string>;
const labels: StatusLabel = {
  todo:        "📌 To Do",
  "in-progress": "🔄 In Progress",
  done:        "✅ Done",
  archived:    "🗄️ Archived",
};
// TypeScript ensures ALL TaskStatus keys are present

// Dynamic string keys:
type PriorityScore = Record<string, number>;
const scores: PriorityScore = { low: 1, medium: 2, high: 3 };

// ── noUncheckedIndexedAccess - index returns T | undefined ────
// With "noUncheckedIndexedAccess": true in tsconfig:
const map: Record<string, number> = { a: 1, b: 2 };
const value = map["c"];       // Type: number | undefined (not just number)
if (value !== undefined) {
  console.log(value * 2);     // ✅ safe access
}

// ── Task Manager: task map by ID ──────────────────────────────
const taskMap: Record<number, Task> = {};
function addToMap(t: Task): void {
  taskMap[t.id] = t;
}
function getFromMap(id: number): Task | undefined {
  return taskMap[id];   // returns Task | undefined with noUncheckedIndexedAccess
}

Common Mistakes

  • Using `[key: string]: any` - default to `[key: string]: unknown` for dynamic data. It forces narrowing before use, catching actual type errors.
  • Not enabling `noUncheckedIndexedAccess` - without it, `record[key]` returns `T` even though the key might not exist. Enable it in tsconfig for safer dictionary access.
  • Using index signatures when the key set is known - if all possible keys are known, use a mapped type or Record with a union literal. Index signatures for known keys bypass excess property checking.

Tip

Tip

Practice Index Signatures Record Types in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and concepts for Index Signatures & Record Types

Practice Task

Note

Practice Task - (1) Write a working example of Index Signatures Record Types from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Index Signatures Record Types is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready typescript code.

Key Takeaways

  • Index signatures define types for dynamic property access - when you don't know all property names ahead of time.
  • Using `[key: string]: any` - default to `[key: string]: unknown` for dynamic data. It forces narrowing before use, catching actual type errors.
  • Not enabling `noUncheckedIndexedAccess` - without it, `record[key]` returns `T` even though the key might not exist. Enable it in tsconfig for safer dictionary access.
  • Using index signatures when the key set is known - if all possible keys are known, use a mapped type or Record with a union literal. Index signatures for known keys bypass excess property checking.

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/8 done