Map an Object's Values, Keeping Its Keys

Write the code, see it run, pass the checks.

The problem

  • medium
  • Code

Write mapValues(obj, fn): return a new object with the same keys as obj, where each value is fn(value, key), leaving obj unchanged. Type it so the result keeps obj's exact keys with fn's return type as every value, and so fn is given the object's value type and its keys as a union of the actual key names, not string.

Your code must pass

  • Maps each value, keeping the keys
  • Passes each key to fn
  • Leaves obj unchanged
  • Has no type errors
  • Keeps the object's exact keys
  • Values have fn's return type
  • fn gets the values and keys typed