javascript - express typescript err throw 'any' 警告
问题描述
我有一小段代码
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}`);
});
我想知道为什么会弹出这个消息
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)
我没有为 req 和 res 声明类型,但是为什么 typescript 只为 err 显示错误?
解决方案
没有错误参数。
按住 CTRL 然后点击listen
它将打开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;
如您所见,callback
没有参数
推荐阅读
- java - 如何自动递增并为 firebase 数据库中的树创建索引 ID?
- javascript - 尝试在 vue 路由器中加载多个模块作为中间件将不起作用
- python - 将时间格式从“20210730T220000Z”转换为“2021-07-30T00:00”格式
- vba - 在word中添加超链接后如何在VBA中重置样式
- optimization - 如何通过以下代码进行优化?它在 Kaggle 中显示内存错误
- javascript - 为什么我在 React 中收到“TypeError: x.map is not a function”?
- bash - 如果文件不为空,则获取退出代码 1
- javascript - Nodejs + mySQL 数据没有发送到我的数据库
- oracle - 我正在尝试在本地安装 Apex 5.1,但我无法以 sys 用户身份从 Apex 目录连接(错误:ORA-01031:权限不足)
- supercollider - 超级对撞机 一切正常时没有声音