这里的目标是从具有argument combination个函数的2个函数中创建一个函数.它可以很容易地用JavaScript完成,但用TypeScrip就不容易了.
组合两个函数的一种方法是Proxy.示例:
const f1 = (a: number) => {
return (a + 1) * 2;
}
const f2 = (b: string, c: string[]) => {
c.push(b);
return c;
}
const combiner = (f1: Function, f2: Function) => {
return new Proxy(f1, {
apply(target, thisArg, args) {
const f1_args = args.slice(0, f1.length);
const f2_args = args.splice(f1.length);
f1(...f1_args);
f2(...f2_args);
}
});
};
const f = combiner(f1, f2);
f(1, 'foo', ['bar']); // Typehints?
问题是could it be typed with typescript to typehint result of 100 function?