Course contentsShow
← TypeScript
Lesson 683 of 1,79616. Utility TypesFree lesson

Record for Status Maps and Lookup Tables

Apply Record to model state machines, configuration objects, and other key-value mappings.

Record for Status Maps and Lookup Tables

You'll learn how to use Record<K, T> to create type-safe configuration objects, state machines, and other key-value structures where every key must have a consistent value type.

The Core Idea

When you have a fixed set of keys (like statuses, error codes, or action types) and each key should map to the same shape of value, Record<K, T> ensures you don't miss any keys and that all values match the expected type. Think of it as a checklist that TypeScript enforces—every item must be accounted for with the correct information.

Status Maps

type OrderStatus = 'pending' | 'shipped' | 'delivered' | 'cancelled';

const statusMessages: Record<OrderStatus, string> = {
  pending: 'Your order is being processed',
  shipped: 'Your order is on the way',
  delivered: 'Your order has arrived',
  cancelled: 'Your order was cancelled'
};
// TypeScript ensures all four statuses are present

Lookup Tables with Consistent Structure

type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE';

const methodConfig: Record<HttpMethod, { requiresBody: boolean }> = {
  GET: { requiresBody: false },
  POST: { requiresBody: true },
  PUT: { requiresBody: true },
  DELETE: { requiresBody: false }
};

If you forget a method or misspell one, TypeScript catches it immediately.

Simple State Machines

type TrafficLight = 'red' | 'yellow' | 'green';

const nextState: Record<TrafficLight, TrafficLight> = {
  red: 'green',
  yellow: 'red',
  green: 'yellow'
};

const current: TrafficLight = 'red';
const next = nextState[current]; // Type: TrafficLight

Key Takeaway: Record<K, T> is perfect for exhaustive mappings where every possible key must be defined with a value of the same type—ideal for configuration objects, state transitions, and lookup tables that need compile-time completeness guarantees.