JS Built-ins
TypeScriptbeginner

Utility Types β€” Record<K, V>

πŸ“– Usage Example
type Scores = Record<string, number>
const scores: Scores = { alice: 95, bob: 87 }

Record<K, V> constructs an object type whose keys are K and whose values are all V.

Utility Types: Record<K, V>

Record<K, V> creates an object type with keys of type K and values of type V.

Challenge: Implement buildRecord<K extends string, V>(keys: K[], value: V): Record<K, V> that creates an object mapping every key to value.

buildRecord(['a', 'b', 'c'], 0)  // β†’ { a: 0, b: 0, c: 0 }
πŸ’‘ Hints (1)
  • β€’ Loop through keys and assign the same value to each.