I am seeking assistance in creating a function where the caller must provide an associative array of functions. The function should return a new associative array with the same keys and return types, but each function will take a different argument compared to the original map.
My challenge lies in defining the return type for this function.
So far, I have attempted:
type FunctionMap<A> = {
[functionName: string]: <R>(a: A) => R
}
type B = {};
const b: B = {};
const FunctionMapArg: FunctionMap<B> = {
getA: b => 1,
getB: b => "two"
};
type TypeOfFunctionMapArg = typeof FunctionMapArg;
type ReturnedFunctionMap<T extends TypeOfFunctionMapArg> = {
[P in keyof T]: () => ???; // using Typescript 2.6
// [P in keyof T]: () => ReturnType<T[P]); // using Typescript 2.8
}
However, I encounter issues right from the start - I cannot even declare the FunctionMapArg
constant without receiving the following compiler error:
Type '{ getA: <R>(a: any) => number; getB: <R>(a: any) => string; }' is not assignable to type 'FunctionMap<any>'.
Property 'getA' is incompatible with index signature.
Type '<R>(a: any) => number' is not assignable to type '<R>(a: any) => R'.
Type 'number' is not assignable to type 'R'.
If anyone could guide me on the correct path, especially one that is compatible with Typescript 2.6, it would be greatly appreciated.