簡體   English   中英

無法編譯 TypeScript - 沒有與此調用匹配的重載

[英]Unable to compile TypeScript - No overload matches this call

我正在使用帶有 NodeJS 和 express 的打字稿。

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

他們上面的代碼在 Visual Studio 代碼中以及當我嘗試編譯打字稿時給了我錯誤。 當我嘗試編譯打字稿時,我收到以下錯誤消息。

沒有重載匹配這個調用。 最后一次重載給出了以下錯誤。

'(err: any) => void' 類型的參數不可分配給類型 '() => void'.ts(2769) 的參數

index.d.ts(1205, 5):這里聲明了最后一個重載。 塊引用

我已經嘗試了下面的代碼:我仍然遇到同樣的錯誤。

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

我似乎無法理解為什么會收到此錯誤。 提前致謝。

有五種可用於listen方法的重載。 而且,他們都沒有將回調視為(error) => void 見這里: https : //github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/express-serve-static-core/ts4.0/index.d.ts#L1110-L1115

永久鏈接,如果上面的鏈接不起作用


    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;

因此,您必須執行以下操作:

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

或者可能

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM