简体   繁体   中英

Unable to compile TypeScript - No overload matches this call

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.

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM