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
Runtime & versions
Compiled to JavaScript. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
TypeScript Handbook