I've encountered a rather intriguing problem that I'm struggling to solve.
My goal is to develop a function that accepts an object of functions as input and returns an object with the same keys but a different return type, which depends on the values passed in as arguments.
For instance:
declare function add(a: number, b: number): number
declare function str(a: string, b: string): string
declare function createObject(obj)
const result = createObject({
addFn: add,
strFn: str
})
/*
The desired TYPE for result should be:
{
addFn: [number, (a: number, b: number) => number],
strFn: [string, (a: string, b: string) => string]
}
*/
I believe this problem can be solved, but I am unsure about the approach. The closest solution I have come up with so far is shown below:
type GenericHashTable<T> = { [key in keyof T]: T[key] };
function createAPI<T extends { [k: string]: any }>(fetchers: T) {
const obj: GenericHashTable<T> = fetchers;
return obj;
}
However, this implementation does not allow me to change the return type effectively.