JS Built-ins
TypeScriptintermediate

Utility Types β€” transformRecord<T, U>

πŸ“– Usage Example
transformRecord({ a: 1, b: 2 }, v => v * 10)   // β†’ { a: 10, b: 20 }

transformRecord maps values in a Record from one type to another while preserving all keys.

Utility Types: Mapped Object Transformation

Challenge: Implement transformRecord<T extends Record<string, unknown>, U>(obj: T, fn: (key: keyof T, value: T[keyof T]) => U): Record<keyof T, U> transforming each value with access to both key and value.

transformRecord({ a: 1, b: 2 }, (k, v) => `${k}=${v}`)
// β†’ { a: 'a=1', b: 'b=2' }
πŸ’‘ Hints (1)
  • β€’ Access both key and value in the transform function.