Record and index signatures

Record and index signatures for key-to-value object types, including missing keys and the lack of runtime validation.

Record<K, T> builds an object type whose keys are K and whose values are T. It is equivalent to a mapped index signature { [P in K]: T }. It is a compile-time type, not a runtime map, and not JavaScript's Map object.

Record does not validate values at runtime. Assigning the result of JSON.parse to a Record type is a type assertion in all but name: the compiler trusts the annotation, and the emitted JavaScript does not check the data.

Examples were checked with TypeScript 5.9.3 using tsc --strict. See the TypeScript handbook for the complete utility-type catalog.

TypeScript reference overview →

API index

APISummary
RecordConstruct an object type with keys K and values T. This is a type-level mapped type. It does not construct a value and it does not check data at runtime.

Record and index signatures

When K is a finite union of string literals, every key in that union is required. Record<"http" | "https", number> needs both http and https. When K is string, the type is an open index signature: any string key is allowed, and missing keys are not a compile-time error.

At runtime, JavaScript object keys are strings (or symbols). A number in a type position is still a property name, not a numeric index check, and Record never inspects the JavaScript value.

Record

type Record<K extends keyof any, T> = {
    [P in K]: T;
};

Construct an object type with keys K and values T. This is a type-level mapped type. It does not construct a value and it does not check data at runtime.

Type parameterDescription
KA key type, constrained to keyof any (string | number | symbol). A literal union lists required keys. string describes an open dictionary.
TThe value type for each key. Record does not accept a per-key value type; use a plain object type or a mapped type when keys need different value types.

Produces: An object type. No runtime object is created by the alias itself.

Type errors

  • type error — When K is a literal union, omitting one of those keys is a type error. Assigning a value of the wrong type to a known key is also a type error. JSON.parse is typed as any, so a later annotation of Record<string, number> is not checked against the parsed payload.
  • In this example, JSON.parse('{"port":"80"}') is annotated as Record<string, number>, but the runtime value of port is the string "80". typeof confirms there is no runtime check.
  • To reject that payload, check the value at runtime, as in the overview recipe on validating parsed JSON. A type annotation is not a validator.
typescript
type Ports = Record<"http" | "https", number>;
const ports: Ports = { http: 80, https: 443 };
// @ts-expect-error Record requires a property for every key in the union
const missing: Ports = { http: 80 };

const counts: Record<string, number> = { a: 1 };
counts.b = 2;

const parsed: Record<string, number> = JSON.parse('{"port":"80"}');
console.log(ports.https, counts.b, typeof parsed.port, parsed.port);

Expected output

443 2 string 80

Related: Validate parsed JSON at runtime, Official Record

Official documentation →