I am faced with the challenge of wrapping functions within an object in order to use their return values, all without altering their signature or losing type information.
// An object containing various functions
const functions = { foo, bar, baz }
// Example wrapper function
const doSomething: (result: any) => void;
// Function to wrap each individual function
function wrapper<P extends any[]>(fn: (...params: P) => any) {
return (...params: P) => doSomething(fn(...params));
}
// The main wrap function which preserves type information
function wrap(functions) {
const wrapped = {};
for (const key in functions) {
wrapped[key] = wrapper(functions[key]);
}
return wrapped;
}
const wrapped = wrap(functions);
// How can we retain type information for wrapped and its functions?
Is it a feasible task to maintain type information (including list of functions and their parameters) while wrapping these functions?