Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 我能告诉我的泛型,它应该只是一个函数吗?_Typescript_Typescript2.0 - Fatal编程技术网

Typescript 我能告诉我的泛型,它应该只是一个函数吗?

Typescript 我能告诉我的泛型,它应该只是一个函数吗?,typescript,typescript2.0,Typescript,Typescript2.0,我的主函数中的类型“T[K]”上不存在属性“apply”错误 const main = <T>(root: T) => <K extends keyof T>(method: K, ...args: any[]) => { root[method].apply(this, ...args); }; const main=(root:T)=>(方法:K,…args:any[])=>{ 根[方法]。应用(此,…args); }; 关于诚实,除非作为一个例子

我的主函数中的类型“T[K]”上不存在属性“apply”错误

const main = <T>(root: T) => <K extends keyof T>(method: K, ...args: any[]) => {
  root[method].apply(this, ...args);
};
const main=(root:T)=>(方法:K,…args:any[])=>{
根[方法]。应用(此,…args);
};
关于诚实,除非作为一个例子:

const main = <T>(root: T) => <K extends keyof T>(method: K, ...args: any[]) => {
  (<any>root[method]).apply(this, ...args);
};
const main=(root:T)=>(方法:K,…args:any[])=>{
(

如果我添加另一种类型(编号)的6级-

很有趣的是为什么第一次调用总是出错

在这种情况下,如果使RUN-everything works结果为“foo”和“bar”,但编译器类型脚本产生错误,则可以执行以下操作:

const main = <T extends { [key: string]: Function }>(root: T) => <K extends keyof T>(method: K, ...args: any[]) => {
    root[method].apply(this, args);
};
const main=(root:T)=>(方法:K,…args:any[])=>{
root[method].apply(这个,args);
};
()


请注意,需要一个数组作为其第二个参数,因此您不应该使用spread运算符。

感谢您的回答,我以更新回应(stackoverflow不允许在注释中插入长链接)我认为你根本不需要泛型函数。看来你要找的是非常具体的。有什么我遗漏的吗?以下是我得到的。在新的例子中,更新2你的结构非常具体。让任何解决方案泛型是没有意义的。你会工作得太多,最终得到一个解决方案n这很难理解,可能包含比需要的代码更多的代码,并且比特定解决方案的性能更差。我强烈建议您放弃这个想法。不幸的是,我继承了这段代码,我想键入它。但可能真的需要重新思考。我想如果有可能将其置于另一个条件下on,over operator&类似于
(method:K,…args:any[])
但显然这并不明显。谢谢您可以将其键入,但不需要将其设置为泛型。