Express+TypeScript报错:No overload matches this call及res.status调用问题
TypeScript + Express 接口类型报错解决
问题情况
搭建集成TypeScript的Node.js应用时,给Express接口的req和res参数手动添加Request、Response类型后出现两个报错,按教程步骤操作未解决,怀疑是Node或TypeScript版本更新导致类型不兼容。
报错信息
报错1
No overload matches this call. The last overload gave the following error. Argument of type '(req: Request, res: Response) => Promise<void>' is not assignable to parameter of type 'Application<Record<string, any>>'. Type '(req: Request, res: Response) => Promise<void>' is missing the following properties from type 'Application<Record<string, any>>': init, defaultConfiguration, engine, set, and 63 more.ts(2769) index.d.ts(164, 5): The last overload is declared here.
报错2
This expression is not callable. Type 'Number' has no call signatures.
相关代码
import express from "express"; import NotesModel from "./models/notes"; export const app = express(); app.get("/", async (req: Request, res: Response) => { const notes = await NotesModel.find(); res.status(200).json(notes); });
依赖版本
"devDependencies": { "@eslint/js": "^9.12.0", "@eslint/migrate-config": "^1.3.1", "@types/express": "^5.0.0", "eslint": "^9.12.0", "globals": "^15.10.0", "nodemon": "^3.1.7", "ts-node": "^10.9.2", "typescript": "^5.6.2", "typescript-eslint": "^8.8.1" },
解决方法
1. 正确导入Express的类型定义
代码中未明确导入Express提供的Request和Response类型,导致TypeScript默认使用了DOM API中的同名类型,和Express的类型不匹配。修改导入语句:
import express, { Request, Response } from "express"; import NotesModel from "./models/notes"; export const app = express(); app.get("/", async (req: Request, res: Response) => { const notes = await NotesModel.find(); res.status(200).json(notes); });
2. 修复@types/express版本兼容性
当前使用的@types/express@5.0.0是为Express 5.x(Beta版)准备的类型定义,若项目中安装的是Express 4.x稳定版,会出现类型不兼容问题。
- 方案一:降级
@types/express到适配Express 4.x的版本
执行安装命令:
npm install @types/express@^4.17.21 --save-dev
- 方案二(不推荐生产环境):升级Express到5.x Beta版
执行安装命令:
npm install express@beta --save
内容的提问来源于stack exchange,提问作者Aldrin Torion
相关产品推荐
相关产品推荐

