Record
Record<K, V> is the built-in mapped type for "an object whose keys are K and whose values are V." K is usually a string-literal union or string.
Detailed Explanation
JavaScript objects are often used as maps: a currency to a total, a status to a label, an id to a row. Record is the type of that object.
type Totals = Record<"usd" | "eur", number>;
Every key in the union is required. { usd: 10 } is an error; eur is missing. That is the closed-key form, and it is how you get autocomplete and exhaustiveness when you iterate Object.keys if you also keep a typed key list.
Record<string, number> is the open form: any string key, number values. Reads return number (or number | undefined under noUncheckedIndexedAccess). Writes accept any string. You have lost the list of allowed keys. This is the type of a cache, a histogram, or a JSON object you have not described further.
K may be string, number, symbol, or a union of those. Record<number, V> describes array-like objects more than arrays. Record<symbol, V> is rare. A union of string literals is the usual interesting case.
Record is a mapped type: { [P in K]: V }. It does not copy modifiers from another type; it builds a new one. Optional keys need Partial<Record<K, V>> or { [P in K]?: V }. Readonly keys need Readonly<Record<K, V>>.
A named interface is better than Record<string, unknown> once you know the keys. Record earns its keep when the key set is a union you already have (Record<Status, Label>), or when the key set is truly open.
Record is not Map. A Map<K, V> can use object keys, preserves insertion order as its API contract, and has .get returning V | undefined. A Record is an object. JSON.stringify accepts it. Object.keys returns string[]. Choose Map when keys are not strings, or when missing vs present must be a .get you cannot confuse with a prototype key.
Prototype keys are the classic hole: Record<string, number> still has toString on the chain. obj["toString"] may be a function, not a number, if you did not create the object with Object.create(null) and the type will not save you at runtime. For user-controlled keys, prefer Map.
Code Examples
1. Closed keys — every member required.
// 1. Closed keys — every member required.
type Currency = "usd" | "eur" | "inr";
type Totals = Record<Currency, number>;
const totals: Totals = { usd: 1999, eur: 0, inr: 0 };2. Open keys — a cache.
// 2. Open keys — a cache.
type User = { id: string; email: string };
const cache: Record<string, User> = {};
function remember(user: User): void {
cache[user.id] = user;
}3. Partial Record for a sparse map of a known key set.
// 3. Partial Record for a sparse map of a known key set.
type Flags = Partial<Record<Currency, boolean>>;
const flags: Flags = { usd: true };Common Gotchas
A closed `Record<K, V>` requires every key in `K`
forgetting one is a type error. Use `Partial<Record<K, V>>` if the map is sparse.
`Record<string, V>` reads are not honest about missing keys unless `noUncheckedIndexedAccess` is on
`cache[id].email` can throw. Check, or use `Map`.
`Object.keys` is still `string[]`
it will not give you `Currency[]`. Keep a `const currencies = [...] as const` to iterate.
User-controlled keys on a `Record` can collide with prototype names
use a `Map` or `Object.create(null)` at runtime.