typescript “提供的参数与调用目标的任何签名都不匹配”是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36807212/
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-09-09 07:05:07  来源:igfitidea点击:

What does, "supplied parameters do not match any signature of call target" mean?

typescript

提问by Gabriel Kunkel

I'm working on an Angular (1.4.9) application with TypeScript. I'm doing an $http post and am using a method to trigger an alert for success or failure on a registration page. I realized that I would have to pass in the context of the method I'm using to trigger the alert and so, typically, I would do something like:

我正在使用 TypeScript 开发一个 Angular (1.4.9) 应用程序。我正在做一个 $http 帖子,并且正在使用一种方法在注册页面上触发成功或失败的警报。我意识到我必须在我用来触发警报的方法的上下文中传递,因此,通常,我会执行以下操作:

public showAlert(alertType: string, alertTitle: string, alertMessage: string, alertTimeout: number): void {
    this.alertShow = true;
    this.alertType = alertType;
    this.alertTitle = alertTitle;
    this.alertMessage = alertMessage;
    this.alertTimeout = alertTimeout;
}

public postIt(): void {
    that: any = this; // <-- See, I know what I'm doing.

    var url: string = "/";
    var user: any = {};

    this.$http.post(url, user)
        .then((data: any) => {
            that.showAlert("success", "Yes!", "You are registered.");
        })
        .catch((err: any) => {
            that.showAlert("warning", "Embarrassing...", "Problem on our end. Try again, later.");
        });
}

But, then I'm like, "Isn't TypeScript smart enough to know what I'm doing, here? Aren't the arrow functions taking care of this for me?" So, I switch it around:

但是,然后我想,“在这里,TypeScript 还不够聪明,无法知道我在做什么吗?箭头函数不是在为我解决这个问题吗?” 所以,我切换它:

public postIt(): void {
    // var that: any = this; <-- Boom! ...Commented out!

    var url: string = "/";
    var user: any = {};

    this.$http.post(url, user)
        .then((data: any) => {
            // it doesn't like the "this"
            this.showAlert("success", "Yes!", "You are registered.");
        })
        .catch((err: any) => {
            // it doesn't like the "this"
            this.showAlert("warning", "Embarrassing...", "Problem on our end. Try again, later.");
        });
}

I think I'm pretty smart. TypeScript cooperates. I checked the interpreted JavaScript and it's doing just what I thought it would:

我觉得我挺聪明的。TypeScript 合作。我检查了解释的 JavaScript,它正在做我认为它会做的事情:

myClass.prototype.postIt = function () {
    // var that: any = this;
    var _this = this;
    var url = "/";
    var user = {};
    this.$http.post(url, user)
        .then(function (data) {
        _this.showAlert("success", "Yes!", "You are registered.");
    })
        .catch(function (err) {
        _this.showAlert("warning", "Embarrassing...", "Problem on our end. Try again, later.");
    });

...And my application works just fine. But, the compiler throws me an error: "TS2346: Supplied parameters do not match any signature of call target." Considering the situation, I want to read that like it can't find the method. When I have it the original way it doesn't make this complaint. Could you explain what this error is about, exactly? Why isn't this error preventing compilation?

...我的应用程序运行良好。但是,编译器向我抛出一个错误:“TS2346:提供的参数与调用目标的任何签名都不匹配。” 考虑到这种情况,我想像找不到方法一样阅读它。当我以原始方式拥有它时,它不会提出这种抱怨。你能解释一下这个错误是什么吗?为什么这个错误不会阻止编译?

回答by Stubb0rn

Error message actually explains very well what is wrong. Method showAlertexpects 4 parameters and all of them are required, but inside postItmethod showAlertis called without alertTimeoutargument. To make this compiler error go away you should either pass alertTimeoutvalue when calling showAlertor update method's signature and make last parameter optional:

错误消息实际上很好地解释了错误所在。方法showAlert需要 4 个参数,并且所有参数都是必需的,但是内部postIt方法showAlert是不带alertTimeout参数调用的。要消除此编译器错误,您应该alertTimeout在调用showAlert或更新方法的签名时传递值并使最后一个参数可选:

public showAlert(alertType: string, alertTitle: string, 
    alertMessage: string, alertTimeout?: number): void {
    ...
}

回答by Sibeesh Venu

If you have added a semi colon at the end of any sections, you will see the same error. I was facing the same issue as I added a semi colon at the end of one module in my importssection in app.module.ts.

如果您在任何部分的末尾添加了分号,您将看到相同的错误。我遇到了同样的问题,因为我importsapp.module.ts.

enter image description here

在此处输入图片说明

Just removing a semi colon from RouterModule.forRoot(APP_ROUTES);fixed it for me.

只需从RouterModule.forRoot(APP_ROUTES);为我修复它中删除一个分号即可。

Note: Though it is not related to OP's question, sharing for the other users who land in this page with the same error.

注意:虽然它与OP的问题无关,但与其他出现相同错误的用户共享此页面。