[英]express typescript err throw 'any' warning
I have this small piece of code我有一小段代码
import express from 'express';
const app = express();
const port = 3000;
app.get('/', (req, res) => {
res.send('The sedulous hyena ate the antelope!');
});
app.listen(port, err => { //show error here
if (err) {
return console.error(err);
}
return console.log(`server is listening on ${port}`);
});
I wonder why this message pop up我想知道为什么会弹出这个消息
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)
I didn't declare type for req and res, but why typescript show error only for the err?我没有为 req 和 res 声明类型,但是为什么打字稿只为错误显示错误?
There is no error paramter.没有错误参数。
Hold CTRL and then click on listen
按住 CTRL 然后点击
listen
It will open index.d.ts
and you will see the definition它将打开
index.d.ts
,您将看到定义
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;
As you can see the callback
takes no argument如您所见,
callback
接受任何参数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.