runtime.boot

Full Stack Systems
Production Mindset

CodeWithMihir

Engineering thoughtful products from interface to infrastructure.

CodeWithMihir

TypeScript Tutorial

TypeScript Record Utility Type Explained

Learn how the Record utility type works in TypeScript, including typed dictionaries, fixed key maps, and practical examples.

Welcome back! I am Mihir, and in this lesson we will learn the Record utility type in TypeScript.

Record<K, T> creates an object type where keys are K and values are T.


Basic Record Example

type Scores = Record<string, number>;

const scores: Scores = {
  mihir: 95,
  alex: 88,
};

This means every string key has a number value.


Fixed Keys with Record

type Role = "admin" | "editor" | "viewer";

type RolePermissions = Record<Role, string[]>;

Usage:

const permissions: RolePermissions = {
  admin: ["create", "read", "update", "delete"],
  editor: ["read", "update"],
  viewer: ["read"],
};

All roles must be present.


Missing Keys Cause Errors

Invalid:

const permissions: RolePermissions = {
  admin: ["create"],
  editor: ["read"],
};

viewer is missing.


Value Types Are Checked

Invalid:

const permissions: RolePermissions = {
  admin: "all",
  editor: ["read"],
  viewer: ["read"],
};

Each value must be string[].


Record for Lookup Objects

type Status = "success" | "error" | "loading";

const statusLabels: Record<Status, string> = {
  success: "Success",
  error: "Something went wrong",
  loading: "Loading...",
};

This is cleaner than a long chain of if statements for simple lookup values.


Record vs Index Signature

Index signature:

type Scores = {
  [name: string]: number;
};

Record:

type Scores = Record<string, number>;

They are similar for simple dictionaries. Record is shorter and works nicely with unions of fixed keys.


Quick Recap

  • Record<K, T> creates an object map.
  • K is the key type.
  • T is the value type.
  • It is useful for dictionaries and lookup objects.
  • With union keys, Record requires every key to exist.

Next up, we will learn Exclude and Extract →.