Home > Software engineering >  Unable to compile TypeScript - No overload matches this call
Unable to compile TypeScript - No overload matches this call

Time:11-15

I am using using typescript with NodeJS and express.

    this.app.listen(port, (err: any) => {
      if (err) {
        console.log("err", err)
      } else {
        console.log(`Server is listing on port ${port}`);
      }
    });

They above code gives me error in visual studio code and also when i try to compile typescript. I am getting the following error message when i try to compile the typescript.

No overload matches this call. The last overload gave the following error.

Argument of type '(err: any) => void' is not assignable to parameter of type '() => void'.ts(2769)

index.d.ts(1205, 5): The last overload is declared here. Blockquote

I have tried the below code: I still get the same error.

this.app.listen(port, (err: any, req: express.Request, res: express.Response) 

I could not seem to understand why I am getting this error. Thanks in advance.

CodePudding user response:

There are five overloads available for listen method. And, none of them take callback as (error) => void. See here: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/express-serve-static-core/ts4.0/index.d.ts#L1110-L1115

Permalink, if the above link does not work


    listen(port: number, hostname: string, backlog: number, callback?: () => void): http.Server;
    listen(port: number, hostname: string, callback?: () => void): http.Server;
    listen(port: number, callback?: () => void): http.Server;
    listen(callback?: () => void): http.Server;
    listen(path: string, callback?: () => void): http.Server;
    listen(handle: any, listeningListener?: () => void): http.Server;

So, you will have to do the following:

 this.app.listen(port, () => {
  console.log(`Server is listing on port ${port}`);
});

Or may be

 const server = this.app.listen(port, () => {
    console.log(`Server is listing on port ${port}`);
  });
 server.on('error', e => console.error("Error", e));
  • Related