typescript – Map over an Object’s keys/values, returning a new Object, ala Array#map

In the process of migrating an ancient CoffeeScript codebase to TypeScript, I’m also attempting to abandon lodash/underscore.js-alikes in favour of modern JavaScript-standard-library-features (or shims thereof.)

One thing my codebase makes a lot of use of, though, is map() over Object instead of Array. In lodash, that’s this.

The following is the as-generic-as-I-could-make-it version:

export type Entry<T> = (string, T)
export type EntriesMapper<T, U> = (
   entry: Entry<T>,
   index: number,
   object: Record<string, T>,
) => Entry<U>

// FIXME: Support `Symbol`-keyed objects. Unfortunately, TypeScript themselves seem to have
//        sidestepped this issue; so I'm not sure what I can do, myself:
//           <https://git.io/JuGVg>
export function ObjectMap<T, U>(
   object: Record<string, T>,
   mapper: EntriesMapper<T, U>,
): { (k: string): U } {
   const entries = Object.entries(object)

   const result = entries.map((entry: Entry<T>, index: number) => {
      return mapper(entry, index, object)

   const return_value = Object.fromEntries(result)

   return return_value