Record<K, T>
Constructs an object type whose property keys are K and whose property values are T, useful for dictionaries and lookup maps.
Definition
type Record<K extends keyof any, T> = {
[P in K]: T;
};Examples
type Roles = "admin" | "editor" | "viewer";
const permissions: Record<Roles, string[]> = {
admin: ["read", "write", "delete"],
editor: ["read", "write"],
viewer: ["read"],
};type HttpStatus = 200 | 301 | 404 | 500;
const statusMessages: Record<HttpStatus, string> = {
200: "OK",
301: "Moved Permanently",
404: "Not Found",
500: "Internal Server Error",
};
console.log(statusMessages[404]); // "Not Found"interface Product {
name: string;
price: number;
}
const productCache: Record<string, Product> = {};
function cacheProduct(id: string, product: Product) {
productCache[id] = product;
}
cacheProduct("abc", { name: "Widget", price: 9.99 });Common Use Cases
- 1Type-safe dictionaries and lookup maps
- 2Mapping enum values to configuration objects
- 3Cache structures indexed by string IDs
- 4Ensuring exhaustive handling of union members
- 5API response normalization with entity maps
Understanding Record<K, T>
Record<K, T> is a utility type that constructs an object type with keys of type K and values of type T. It is one of the most versatile types in TypeScript, used extensively for creating dictionaries, lookup tables, and maps.
When K is a union of string literals, Record enforces that every member of the union has a corresponding entry. This makes it excellent for exhaustive mappings—if you add a new role to your Roles union, TypeScript will immediately flag any Record<Roles, ...> that is missing the new key.
Record is commonly used in state management to normalize data. Instead of storing arrays of entities, you can store them in a Record<string, Entity> where each entity is indexed by its ID. This provides O(1) lookups and makes updates straightforward.
For API layers, Record types help define response shapes. For example, Record<string, unknown> is a common type for arbitrary JSON objects, while more specific forms like Record<"data" | "error", any> constrain the expected keys.
Record also works well with numeric keys and symbols. Record<number, T> creates an array-like structure with numeric indices, while Record<symbol, T> allows symbol-keyed properties. The flexibility of the K constraint (keyof any, which is string | number | symbol) means Record can model virtually any object shape.
When combined with other utility types, Record becomes even more powerful. For example, Partial<Record<Roles, Permission>> creates a dictionary where not every role needs to be present.
Related Types
Pick<T, K>Constructs a type by picking the set of properties K from T, creating a subset of the original type.
Omit<T, K>Constructs a type by picking all properties from T and then removing K, the inverse of Pick.
Mapped TypesMapped types iterate over the keys of a type to create a new type by transforming each property, enabling systematic type transformations.
keyof TThe keyof type operator produces a union of all known public property keys of a type, enabling type-safe property access patterns.
More Mapping Types
Explore TypeScript Types
Browse our complete reference of 30 TypeScript utility types with definitions, examples, and explanations.