Currently, I am working on a generic JS function that can wrap any other function. The purpose of this wrapper is to execute the wrapped function, log the input and output events, and then return the output for "transparent" logging. However, as I attempt to transition this function to TypeScript while maintaining type information for both the wrapped function and its output, I am facing some complexity.
Here is my current progress:
const syncLogger = <T>(f: T) => (...args: unknown[]): ReturnType<T> => {
let value;
try {
value = f(...args);
functionLogger('info', f, value, ...args); //logging
} catch (error) {
functionLogger('error', f, error.message, ...args); //logging
throw error;
}
return value;
};
and here is how it should be utilized:
const myLoggedFunction = syncLogger(originalFunction);
The main issue arises with the args
, which are used as the input for functions. I am struggling to find a way to inform TypeScript that these arguments correspond precisely to the parameters of the original function being wrapped.