I have two functions:
export const processCar = (params: IParams): Car => {
// ...
}
export const processBoat = (params: IParams): Boat => {
// ...
}
I have another function to which the first two functions will be passed as arguments:
const processOrNull = (func: any): any => {
const newFunc = (params: IParams): any => {
// Perform some check. If it fails, return null. If it passes...
return func(params);
}
return newFunc;
}
export const processCarOrNull = processOrNull(processCar); // (IParams) => Car | null;
export const processBoatOrNull = processOrNull(processBoat); // (IParams) => Boat | null;
I want the result from processOrNull to be typed as either (IParams) => (Car | null) or (IParams) => (Boat | null), depending on whether the passed function returns a Car or a Boat.
Can I do this with generics and if so, how?