TypeScript optional callback parameter does not match anonymous function passed to it


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


This snippet <a href="http://www.typescriptlang.org/play/index.html#src='use%20strict'%3B%0D%0A%0D%0Aclass%20MyClass%20%7B%0D%0A%20%20%20%20public%20refreshConnection(callback%3F%3A%20Function)%20%7B%0D%0A%20%20%20%20%20%20%20%20if%20(callback)%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20callback()%3B%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%7D%0D%0A%7D%0D%0A%0D%0Alet%20obj%20%3D%20new%20MyClass()%3B%0D%0Aobj.refreshConnection(()%20%3D%3E%20%7B%20console.log('It%20works!')%3B%20%7D)%3B%0D%0A" rel="nofollow">seems to be working</a> fine:

class MyClass { public refreshConnection(callback?: Function) { if (callback) { callback(); } } } let obj = new MyClass(); obj.refreshConnection(() => { console.log('It works!'); });

Can you share more of your code in a TS playground?


  • Module not found: Error: Can't resolve './app.module.ngfactory' - ionic 3, angular 5
  • Intermediate and return values in continuation-passing style
  • Trouble understanding GHC complaint about ambiguity
  • Outlook Com not registered
  • Declare a C++ class without defining it in the current translation unit
  • With one EDMX file use multiple connection strings that relate to multiple databases
  • Using Paypal Pro in Omnipay
  • django server code not updating
  • Using constants or global variables in 3 tier console application
  • Checking a play current mode makes an error occur
  • How to use animated gif in Firemonkey?
  • Problem with Django using Apache2 (mod_wsgi), Occassionally is “unable to import from module” for no
  • Zoom in and out of jPanel
  • Position: fixed nav does not stay fixed
  • Creating Java object from class name with constructor, which contains parameters [duplicate]
  • Firefox Extension - Monitor refresh and change of tab
  • Unable to decode certificate at client new X509Certificate2()
  • Saving Changes After In-App Purchase Has Been Purchased
  • Needing to do .toArray() to get output of mongodb .find() on key name not value
  • preg_replace Double Spaces to tab (\\t) at the beginning of a line
  • Typescript - Unable to get 'import' statement to function
  • Alert pop up with LWUIT
  • Extracting HTML between tags
  • MongoDB in PHP using aggregate to group by _id is null not working
  • Disabling Alt-F4 on a Win Forms NotifyIcon
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • TFS: Get latest causes slow project reloading
  • Symfony2: How to get request parameter
  • To display the title for the current loaction in map in iphone
  • ORA-29908: missing primary invocation for ancillary operator
  • AngularJs get employee from factory
  • Hits per day in Google Big Query
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Linking SubReports Without LinkChild/LinkMaster
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?