Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Utility Types

TypeScript Record<K, T> utility type

Reference for TypeScript Record<K, T>: build an object type from a key type and a value type, with union keys, string indexes and exhaustiveness checks.

Record<K, T> creates an object type whose keys are the members of K and whose values all have type T. When K is a union of string literals, you get one required property per member. When K is string, number or symbol, you get an index signature.

It is the standard way to type a lookup table: a dictionary from ids to objects, a map from status names to labels, or a configuration keyed by an enum-like union.

TypeScript
type Color = "red" | "green" | "blue";
type Hex = Record<Color, string>;
// { red: string; green: string; blue: string }

Try it

TypeScript A table keyed by a union

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript A missing key is an error

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript String keys as a dictionary

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Syntax

TypeScript
type Record<K extends keyof any, T> = {
  [P in K]: T;
};
Name Description
K The key type. Must be assignable to string | number | symbol: a literal union, string, number, symbol, or a template literal type.
T The type of every value.

Result: an object type with a property of type T for each key in K. With a literal union, all keys are required.

Edge cases: Record<string, T> has an index signature, so any key reads as T even when absent (unless noUncheckedIndexedAccess is on). Record<never, T> is {}.

Usage notes

  • Exhaustiveness. With a union key, the compiler forces you to provide every member and reports a missing one. Adding a new member to the union breaks every table that forgot it, which is often exactly what you want.
  • Sparse tables: wrap with Partial, as in Partial<Record<K, T>>, when not all keys must exist.
  • Different value types per key cannot be expressed with Record; write an interface or a mapped type instead.
  • noUncheckedIndexedAccess makes reads from Record<string, T> return T | undefined, which is the honest type for a dictionary.
  • Prototype keys. A plain object literal inherits from Object.prototype, so keys such as toString exist at runtime. For arbitrary user-provided keys, prefer Map or Object.create(null).
  • Compared with an index signature: Record<string, T> and { [key: string]: T } are the same type; Record is shorter and works with generic key types.
  • Key order follows normal JavaScript rules: integer-like keys first, then strings in insertion order.