TypeScript 7 reference · Utility Types
TypeScript Record<Keys, Type> Utility Type
Build an object type whose keys come from Keys and whose values all have Type. Great for lookup tables.
TypeScript 7 Since 2.1
Syntax
Record<Keys, Type>
Parameters
| Parameter | Description |
|---|---|
Keys |
The key type: a union of literals, or string/number. |
Type |
The type of every value. |
Return value
An object type.
Examples
type Status = "draft" | "published" | "archived";
const labels: Record<Status, string> = {
draft: "Draft",
published: "Live",
archived: "Archived",
};
console.log(labels.published);
Output
Live
const stock: Record<string, number> = {};
stock["mug"] = 12;
stock["tea"] = 3;
console.log(Object.entries(stock));
type Status = "draft" | "published";
// @ts-expect-error: every Status key is required
const missing: Record<Status, number> = { draft: 1 };
Output
[ [ 'mug', 12 ], [ 'tea', 3 ] ]
Version history
| Version | Change |
|---|---|
| TypeScript 2.1 | Added |
Related
Pick<Type, Keys>· Build a type from just some properties of another type. Handy for API responses and props that need a subset.keyof· Produce a union of an object type's property names: keyof { a: 1; b: 2 } is 'a' | 'b'.- TypeScript Utility Types · tutorial