What it Does

Creates an object type with a specific set of keys, all having the same value type.

Syntax

type RecordType = Record<KeyType, ValueType>;

Use Cases

Detailed Example


type UserMap = Record<string, string>;
const users: UserMap = {
  "user_1": "Alice",
  "user_2": "Bob",
  "user_3": "Charlie"
};

*// Status code meanings*
type HttpStatusMessages = Record<number, string>;
const statusMessages: HttpStatusMessages = {
  200: "OK",
  404: "Not Found",
  500: "Internal Server Error"
};

*// Role permissions using union type for keys*
type Role = 'admin' | 'editor' | 'viewer';
type RolePermissions = Record<Role, boolean>;
const permissions: RolePermissions = {
  admin: true,
  editor: true,
  viewer: false
  *// All three keys MUST be present*
};

Advanced Example

typescript*// Mapping product SKUs to product info*
type ProductInfo = {
  name: string;
  price: number;
};

type ProductCatalog = Record<string, ProductInfo>;
const catalog: ProductCatalog = {
  "SKU001": { name: "Laptop", price: 999 },
  "SKU002": { name: "Mouse", price: 25 }
};

Key Points