Record Utility Type
What
The Record<K, V> utility type creates a type for an object where the keys are of type K and the values are of type V.
How
You specify the type for the keys and the type for the values:
type PlayerRole = "tank" | "healer" | "dps";
// Every key in PlayerRole must have a number value
const requirements: Record<PlayerRole, number> = {
tank: 1,
healer: 1,
dps: 3,
};
Why
Record is much more powerful than a simple index signature because it can use a union of literal types as keys. This allows the TypeScript compiler to ensure that your object is exhaustive—if you miss a key from the union, or add an extra one, you’ll get an immediate error.
Summary
Use Record<K, V> to build strict mapping objects and lookup tables.