我想在TypeScript的另一个函数中通用地“包装”函数。这是我要执行的操作的一个简单示例:
function wrap(f: Function){
console.log("You called a function!");
// return function with same signature as f
}
function hello(name: string){
return "Hello " + name;
}
function add(n1: number, n2: number){
return n1 + n2;
}
const wrappedHello = wrap(sayHello);
const wrappedAdd = wrap(add);
console.log(wrappedHello("Jim"));
// Outputs:
// You called a function!
// Hello Jim
console.log(wrappedAdd(1, 2));
// Outputs:
// You called a function!
// 3
如何定义wrap
函数,以便它维护参数并为传递给它的任何函数返回类型?