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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

Record<Keys, Type>

Parameters

ParameterDescription
Keys The key type: a union of literals, or string/number.
Type The type of every value.

Return value

An object type.

Examples

A label for every statusTry it yourself
type Status = "draft" | "published" | "archived";
const labels: Record<Status, string> = {
  draft: "Draft",
  published: "Live",
  archived: "Archived",
};
console.log(labels.published);

Output

Live
String keys and missing keysTry it yourself
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

VersionChange
TypeScript 2.1Added

Was this page helpful?