Can TypeScript map an object's values to another type based on the actual type of each entry in the result?
To better explain my goal, consider the following scenario:
const obj = {
a: 1,
b: true,
c: "foo"
}
const result = toFunctions(obj)
// The resulting type would be:
{
a: (input: number) => any // since 'a' was of type number
b: (input: boolean) => any // since 'b' was of type boolean
c: (input: string) => any // since 'c' was of type string
}
I aim to leverage the types from the original object's keys to generate the keys in the returned object while applying some form of transformation. In my case, I intend to encapsulate values in functions, but this mechanism could also yield other aggregate types like number[]
or string[]
.
My objective is to create a generic approach to produce function types based on the keys of any object.