So say I have a simple use case - I want a function to be wrapped in another time tracking function. Rather than copy-paste my time tracking code everywhere I want to abstract the time tracking code by creating a wrapper function around my original function and creating a new function as a result. So I end up with a function like the following.
function wrapFunction(someFunction: Function) {
const wrappedFunction = function() {
let args = arguments;
let beforeTime = new Date();
someFunction.apply(args);
let afterTime = new Date();
console.log("Time Elapsed " + afterTime - beforeTime);
};
return {execute: wrappedFunction};
}
Then to use it I would do something like the following:
//Generate the wrapped function
let wrappedFunction = wrapFunction((param1 : string, param2: number) =>{
console.log("Param1 = ", param1);
console.log("Param2 = ", param2);
});
//This is the actual usage of the function
wrappedFunction.execute('param1', 2);
My question is: is there a way to dynamically set the function parameters of that returned .execute() in a way that Typescript can detect errors, and IDEs can pick up on the function parameters.
In the current state, I have no way to check what parameters I should be passing into .execute() without checking where the function was generated.