有没有办法在 Typescript 的包装函数中动态键入函数?
Is there a way to dynamically type a function in a wrapped function in Typescript?
假设我有一个简单的用例——我想要一个函数被包装在另一个时间跟踪函数中。我不想在任何地方复制粘贴我的时间跟踪代码,而是想通过围绕我的原始函数创建一个包装函数并因此创建一个新函数来抽象时间跟踪代码。所以我最终得到了如下函数。
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?
}
您可以在剩余参数中使用泛型和元组:
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: 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?
}
您可以在剩余参数中使用泛型和元组:
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