While attempting to create a generic wrapper function, I initially thought the following code would suffice, but unfortunately, it is not functioning as expected:
function wrapFunctionWithLogging<T extends Function>(f: (...args: Parameters<T>) => ReturnType<T>): ReturnType<T> {
return function (...args: Parameters<T>) {
console.log('before ');
try {
return f(...args);
} catch (error) {
console.error(error);
throw error;
} finally {
console.log('done');
}
};
}
Subsequently, I tried an alternative version using two types:
wrapFunctionWithLogging<TArgs extends any[], TReturn>
This alternative version seems to be working fine, leaving me inquisitive as to why the initial version is not functioning properly.