object-transforms

Record<K, V>

Creates an object type whose keys come from the union K and whose values all have type V. Useful for lookup tables, dictionaries, and enum-keyed maps.

type Record<K extends keyof any, V> = { [P in K]: V }

Usage patterns

Pattern Purpose
Record<string, T> Loose dictionary / index signature
Record<'a' | 'b', T> Exhaustive map — every listed key required
Record<Enum, T> Force a value for every enum member
Partial<Record<K, V>> Sparse dictionary — keys optional

Examples

type Roles = 'admin' | 'user' | 'guest';
type Permissions = Record<Roles, string[]>;

TS will error if any of the three role keys is missing

const cache: Record<string, User> = {};
cache['42'] = user;

Basic string-keyed dictionary — every access returns User (or undefined with noUncheckedIndexedAccess)

type PageMap = Record<number, string>;

Numeric-keyed lookup — TS also allows number keys as strings at runtime

type SparseMap = Partial<Record<'a' | 'b' | 'c', number>>;

Now each key is optional: { a?; b?; c? }

Gotcha

With `Record` without noUncheckedIndexedAccess, accessing a missing key returns V rather than V | undefined — hiding real bugs. Prefer enabling that flag or narrow K to a finite union.

Related utility types

← All TS utility types · TypeScript vs JavaScript · any vs unknown vs never