What is the built-in Record<K, V> utility type, and how does it relate to an index signature?
Short answer
Record is a built-in utility type equivalent to `{ [key in K]: V }` — it's the more concise, commonly preferred way to express a dictionary-like object type where K is typically a string, number, or union of literal types and V is the value type.
Advertisement
AModel answer
Record<K, V> is a built-in utility type equivalent to `{ [key in K]: V }` — it's the more concise, commonly preferred way to express a dictionary-like object type where K is typically a string, number, or union of literal types and V is the value type.
type Scores = Record<string, number>;
const scores: Scores = { alice: 90, bob: 85 };
type Role = 'admin' | 'user' | 'guest';
type RolePermissions = Record<Role, string[]>;Real-world example
Typing a lookup table keyed by a known finite set of literal values, like Record<Role, Permissions>.
Likely follow-ups
- How does Record<K, V> behave differently when K is a union of literals versus a plain 'string'?
Related topics