Javascript 有没有办法在Typescript中的包装函数中动态键入函数?
假设我有一个简单的用例——我希望一个函数包装在另一个时间跟踪函数中。我不想在任何地方复制粘贴我的时间跟踪代码,而是想通过围绕原始函数创建一个包装函数并创建一个新函数来抽象时间跟踪代码。最后我得到了一个如下的函数Javascript 有没有办法在Typescript中的包装函数中动态键入函数?,javascript,typescript,function,types,metaprogramming,Javascript,Typescript,Function,Types,Metaprogramming,假设我有一个简单的用例——我希望一个函数包装在另一个时间跟踪函数中。我不想在任何地方复制粘贴我的时间跟踪代码,而是想通过围绕原始函数创建一个包装函数并创建一个新函数来抽象时间跟踪代码。最后我得到了一个如下的函数 function wrapFunction(someFunction: Function) { const wrappedFunction = function() { let args = arguments; let beforeTime = new Date()
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};
}
然后要使用它,我会做如下操作:
//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);
我的问题是:是否有一种方法可以动态设置返回的.execute()的函数参数,使Typescript可以检测错误,IDE可以检测函数参数
在当前状态下,如果不检查函数的生成位置,我无法检查应传递给.execute()的参数。是的,请使用泛型函数类型:
function wrapFunction<F extends Function>(someFunction: F) {
function wrappedFunction(...args) { // personal preference + performance boost
const beforeTime = new Date();
const result = someFunction.apply(this, args);
const afterTime = new Date();
console.log("Time Elapsed " + afterTime - beforeTime);
return result;
}
return {execute: wrappedFunction as F }; // why this object though?
}
函数包装函数(someFunction:F){
函数wrappedFunction(…参数){//个人偏好+性能提升
const beforeTime=新日期();
const result=someFunction.apply(此参数为args);
常量后时间=新日期();
日志(“已用时间”+后时间-前时间);
返回结果;
}
返回{execute:wrappedFunction as F};//但为什么是这个对象?
}
您可以在rest参数中使用泛型和元组:
function wrapFunction<A extends any[], R>(someFunction: (...a: A) => R) {
const wrappedFunction = function (...args: A) {
let beforeTime = new Date();
let result = someFunction(...args);
let afterTime = new Date();
console.log("Time Elapsed " + (afterTime.getDate() - beforeTime.getDate()));
return result;
};
return { execute: wrappedFunction };
}
//Generate the wrapped function
let wrappedFunction = wrapFunction((param1: string, param2: number) => {
console.log("Param1 = ", param1);
console.log("Param2 = ", param2);
});
wrappedFunction.execute('param1', 2); //ok now
function-wrapFunction(someFunction:(…a:a)=>R){
const wrappedFunction=函数(…参数:A){
let beforeTime=新日期();
设result=someFunction(…args);
让后时间=新日期();
log(“经过的时间”+(beforeTime.getDate()-beforeTime.getDate());
返回结果;
};
返回{execute:wrappedFunction};
}
//生成包装函数
让wrappedFunction=wrapFunction((param1:string,param2:number)=>{
console.log(“Param1=”,Param1);
console.log(“Param2=”,Param2);
});
wrappedFunction.execute('param1',2)//好了
我不太愿意直接使用F
,这意味着execute
中保留了任何额外的属性,但它们没有。您仍然可以使用此解决方案,但使用ReturnType
和参数创建一个签名,该签名将不包含F
的任何额外属性。titian是的,但我想这在使用上可能有些过头了,但由于typescript现在允许我们直接将内容分配给函数,并将这些内容直接发送给函数类型,因此可能会导致混淆。示例:感谢您的回复!不幸的是,这不允许我的IDE拾取函数参数,但它确实执行了我想要的类型!不确定IDE是否可行,但这已经足够接近了!