JS Built-ins
TypeScriptintermediate

Generics β€” mapValues<T, U>

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

mapValues<T, U> transforms every value in a Record, preserving keys and type-checking the result.

Generics: mapValues

Challenge: Implement mapValues<T, U>(obj: Record<string, T>, fn: (v: T) => U): Record<string, U> transforming every value.

mapValues({ a: 1, b: 2 }, x => x * 2)   // β†’ { a: 2, b: 4 }
mapValues({ x: 'hi' }, s => s.length)   // β†’ { x: 2 }
πŸ’‘ Hints (1)
  • β€’ Object.keys gives all own enumerable keys.