Javascript 提供的参数与包装器方法中调用目标的任何签名都不匹配 - Typescript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25631801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 21:37:54  来源:igfitidea点击:

Supplied parameters do not match any signature of call target in wrapper method - Typescript

javascripttypescript

提问by John

Ok I'm guessing I'm missing something really simple on this one.

好吧,我猜我在这个上遗漏了一些非常简单的东西。

Lets say I have multiple methods that repeat a lot of the same things like this:

假设我有多种方法可以重复很多类似的事情:

    public getDepartments(id: number): ng.IPromise<IDepartmentViewModel[]> {
        this.common.loadStart();
        return this.unitOfWork.teamRepository.getDepartmentsForTeam(id).then((response: IDepartmentViewModel[]) => {
            this.common.loadComplete();
            return response;
        }).catch((error) => {
                this.common.loadReset();
                return error;
            });
    }

Tons of boilerplate for a single call to this.unitOfWork.teamRepository.getDepartmentsForTeam(id)

一次调用的大量样板文件 this.unitOfWork.teamRepository.getDepartmentsForTeam(id)

so I wanted to make a generic wrapper for the boilerplate such as:

所以我想为样板制作一个通用包装器,例如:

private internalCall<T>(method: () => ng.IPromise<T>): ng.IPromise<T> {
        this.common.loadStart();
        return method().then((response: T) => {
            this.common.loadComplete();
            return response;
        }).catch((error) => {
            this.common.loadReset();
            return error;
        });
    }

Which I could then call like:

然后我可以称之为:

public getDepartments(id: number): ng.IPromise<IDepartmentViewModel[]> {
        return this.internalCall<IDepartmentViewModel[]>(this.unitOfWork.teamRepository.getDepartmentsForTeam(id));

But I get the following error:

但我收到以下错误:

Supplied parameters do not match any signature of call target:
Type '() => ng.IPromise<IDepartmentViewModel[]>' requires a call signature, but type 'ng.IPromise<IDepartmentViewModel[]>' lacks one.

What is the right way to pass my method into the other to call it with supplied parameters?

将我的方法传递给另一个方法以使用提供的参数调用它的正确方法是什么?

采纳答案by John

I needed to wrap the call so it was wrapped in a closure like so:

我需要包装调用,所以它被包装在一个闭包中,如下所示:

public getDepartments(id: number): ng.IPromise<IDepartmentViewModel[]> {
    return this.internalCall<IDepartmentViewModel[]>(
        () => { return this.unitOfWork.teamRepository.getDepartmentsForTeam(id); } // Wrapping here too
    );

回答by micurs

This is a common mistake: you cannot pass a method function as a regular function since it requires the instance for the class as context. The solution is to use a closure:

这是一个常见的错误:您不能将方法函数作为常规函数传递,因为它需要类的实例作为上下文。解决方案是使用闭包:

function foo( func: () => any ) {
}
class A {
 method() : any {
 }
}
var instanceOfA = new A;

// Error: you need a closure to preserve the reference to instanceOfA
foo( instanceOfA.method ); 
// Correct: the closure preserves the binding to instanceOfA 
foo( () => instanceOfA.method() ); 

For a more complete example you can also see my snippetpublished here: http://www.snip2code.com/Snippet/28601/Typescript--passing-a-class-member-funct

有关更完整的示例,您还可以在此处查看我发布的代码段http: //www.snip2code.com/Snippet/28601/Typescript--passing-a-class-member-funct

回答by El Dude

Only for documentation - I got this error when I accidentally called the wrong (existing) function with wrong parameters. Had to look into the errorous line in the packaged file .tmp/bla/bla/bla.tsto see the error.

仅用于文档 - 当我不小心使用错误参数调用错误(现有)函数时出现此错误。必须查看打包文件.tmp/bla/bla/bla.ts中的错误行才能看到错误。

回答by Arun Giri

Try replacing your fat arrow in to normal function. This will resolve the issue. () => ng.IPromise to function(){ng.IPromise .....}

尝试将您的胖箭头替换为正常功能。这将解决问题。() => ng.IPromise to function(){ng.IPromise .....}

回答by JaNe

In my case a simpler trick allowed me to dodge the error. The call (or trigger)of a function is due to it parentheses, so :

在我的情况下,一个更简单的技巧让我能够避开错误。函数的调用(或触发器)是由于它的括号,所以:

class MyClass {
foo: any;

  firstMethod() {
     this.foo = this.secondMethod;
     this.foo();
  }

  secondMethod() {
  }
}

回答by jmojico

In a more generic answer, the error "Supplied parameters do not match any signature of call target in wrapper method - Typescript"points out that you are calling a function with the wrong parameters.

在更通用的答案中,错误“提供的参数与包装器方法 - Typescript 中调用目标的任何签名不匹配”指出您正在使用错误的参数调用函数。

example() receives two parameters per definition, but you are passing only one:

example() 每个定义接收两个参数,但您只传递一个:

example('param1') // wrong
example('param1','param2') // OK!