![](/img/trans.png)
[英]I am new in typescript When I compile my code given No overload matches this call error
[英]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.