6

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 component, 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.component.ts
Module build failed: Error: /var/www/mysite/frontend/angular2/src/app/fb_connect.component.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

3
  • I cannot reproduce. Could you try to use that site to reproduce your error, then edit your question to add the link? 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. Commented Jan 12, 2017 at 22:30
  • I dont use resharper, at least not knowingly. The project is an Angular 2 app, and i compile it using "ng build" tool...which in turn uses webpack. I added the error code to the original post. Note: when i comment out the function call, everything works fine. I also tried emptying out the function( having a blank function that does nothing. ) I think the compiler maybe giving me a wrong line number. Commented Jan 12, 2017 at 22:56

1 Answer 1

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!'); });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.