我有错误属性'申请'在我的主要功能
中的类型&#39; T [K]&#39; 上不存在const main = <T>(root: T) => <K extends keyof T>(method: K, ...args: any[]) => {
root[method].apply(this, ...args);
};
诚实,除了作为一个例子:
const main = <T>(root: T) => <K extends keyof T>(method: K, ...args: any[]) => {
(<any>root[method]).apply(this, ...args);
};
更新
我可能没有这么说,但我的意思是这个例子......
更新2
第二次调用level3和方法level4 / 5(仅限) - works perfectly!
如果我添加另一种类型(数字)的等级6 - everything collapses :(
为什么第一次调用错误是非常有趣的?
在这种情况下,如果你进行RUN - 一切正常的结果都是&#39; foo&#39;和&#39; bar&#39;,但编译器打字稿会产生错误
答案 0 :(得分:0)
你可以这样做:
const main = <T extends { [key: string]: Function }>(root: T) => <K extends keyof T>(method: K, ...args: any[]) => {
root[method].apply(this, args);
};
请注意Function.prototype.apply()期望数组作为其第二个参数,因此您不应该使用扩展运算符。