最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeScript optional callback parameter does not match anonymous function passed to it - Stack Overflow

programmeradmin1浏览0评论

I have a simple problem with my TS callbacks.

I have a function like this

...
//inside a class
    //function is supposed to optionally accept any callback function
    refreshConnection(callback?:Function) {
        //do something
        //then call the passed callback with no params
        callback();
    }

...

//in another ponent, i call this function like so
this.myclass.refreshConnection( () => {
    window.location.reload();
});

//but i get an error saying that the function parameter does not match a signature.

// i also tried callback?: (...args: any[]) => any but nothing.


ERROR in ./src/app/fb_connectponent.ts
Module build failed: Error: /var/www/mysite/frontend/angular2/src/app/fb_connectponent.ts (70,41): Supplied parameters do not match any signature of call target.)
    at _checkDiagnostics (/var/www/mysite/frontend/angular2/node_modules/@ngtools/webpack/src/loader.js:115:15)
    at /var/www/mysite/frontend/angular2/node_modules/@ngtools/webpack/src/loader.js:140:17
 @ ./src/app/app.module.ts 15:0-51
 @ ./src/app/index.ts
 @ ./src/main.ts  

Note: (70,41) is the function call for refreshConnection. Commenting it out fixes the problem

I have a simple problem with my TS callbacks.

I have a function like this

...
//inside a class
    //function is supposed to optionally accept any callback function
    refreshConnection(callback?:Function) {
        //do something
        //then call the passed callback with no params
        callback();
    }

...

//in another ponent, i call this function like so
this.myclass.refreshConnection( () => {
    window.location.reload();
});

//but i get an error saying that the function parameter does not match a signature.

// i also tried callback?: (...args: any[]) => any but nothing.


ERROR in ./src/app/fb_connect.ponent.ts
Module build failed: Error: /var/www/mysite/frontend/angular2/src/app/fb_connect.ponent.ts (70,41): Supplied parameters do not match any signature of call target.)
    at _checkDiagnostics (/var/www/mysite/frontend/angular2/node_modules/@ngtools/webpack/src/loader.js:115:15)
    at /var/www/mysite/frontend/angular2/node_modules/@ngtools/webpack/src/loader.js:140:17
 @ ./src/app/app.module.ts 15:0-51
 @ ./src/app/index.ts
 @ ./src/main.ts  

Note: (70,41) is the function call for refreshConnection. Commenting it out fixes the problem

Share Improve this question edited Jan 12, 2017 at 22:58 Rainer Plumer asked Jan 12, 2017 at 22:16 Rainer PlumerRainer Plumer 3,7533 gold badges28 silver badges42 bronze badges 3
  • I cannot reproduce. Could you try to use that site to reproduce your error, then edit your question to add the link? – Madara's Ghost Commented Jan 12, 2017 at 22:22
  • Are you using resharper? I get so many incorrect errors like this with resharper... it's ridiculous. I sometimes navigate files with a sea of red. – David Sherret Commented Jan 12, 2017 at 22:30
  • I dont use resharper, at least not knowingly. The project is an Angular 2 app, and i pile it using "ng build" tool...which in turn uses webpack. I added the error code to the original post. Note: when i ment out the function call, everything works fine. I also tried emptying out the function( having a blank function that does nothing. ) I think the piler maybe giving me a wrong line number. – Rainer Plumer Commented Jan 12, 2017 at 22:56
Add a ment  | 

1 Answer 1

Reset to default 8

This snippet seems to be working fine:

class MyClass {

    public refreshConnection(callback?: Function) {

        if (callback) {
            callback();
        }
    }
}

let obj = new MyClass();
obj.refreshConnection(() => { console.log('It works!'); });
发布评论

评论列表(0)

  1. 暂无评论