'Deep transform of values in typescript object graphs

Given an object graph of arbitrary depth, I would like to transform values at specific "object paths", while mapping the transformed value types correctly.

example:

let object = {
  foo: {
    bar: {
      baz: 123,
    },
  },
  thing: [
    {
      deeperThing: 'hello',
    },
  ],
}

// i want a to be of type { foo: { bar: string }, ... }
const a = transformAt(object, ['foo.bar'], () => 'object is now a string')
// i want b to be of type { thing: { deeperThing: string[] }[], ... }
const b = transformAt(object, ['thing[0].deeperThing'], (v) => [`${v} world`])

I'm most interested in how to handle the type mapping. Any ideas?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source