Codectionary / Developer documentation / TypeScript

Record<K, V>

Record<Keys, ValueType> constructs an object type with a specific set of keys, all mapped to the same value type. It is the concise, standard way to type dictionary-like objects, and is often clearer than writing an equivalent index signature by hand.

Syntax

Record<KeyType, ValueType>

Examples

A Basic Record

Typing a dictionary object with a known key set.

type Role = "admin" | "editor" | "viewer";

const permissions: Record<Role, string[]> = {
  admin: ["read", "write", "delete"],
  editor: ["read", "write"],
  viewer: ["read"]
};

// TypeScript ensures ALL three roles are present with the correct value type
console.log(permissions.admin); // ["read", "write", "delete"]

Record with String Keys

A more open dictionary type, equivalent to an index signature but more concise.

const scores: Record<string, number> = {
  alice: 95,
  bob: 87,
  charlie: 92
};

// Equivalent to writing:
// interface Scores { [name: string]: number; }

Best practices

  • Use Record<Keys, ValueType> as the concise, preferred way to type dictionary-style objects, rather than writing an index signature by hand
  • Use a specific literal union for Keys (like Record<Role, string[]>) when you want TypeScript to enforce that every possible key is actually present
  • Use Record<string, ValueType> for a genuinely open-ended dictionary where keys are not known ahead of time
  • Prefer Map over Record when keys are added/removed dynamically at runtime - Record is best suited for a fixed, known shape

At a glance

Purpose
Static types for JavaScript
File extension
.ts ยท .tsx
Runs in
Compiled to JavaScript
Usually used with
JavaScript and its ecosystem

Specifications & further reading

Related TypeScript documentation