使用TypeScript搭建Express服务时出现No overload matches this call错误
问题:TypeScript搭建Express服务器时路由报错
错误信息
No overload matches this call. The last overload gave the following error. Argument of type '(req: Request, res: Response) => express.Response<any, Record<string, any>>' is not assignable to parameter of type 'Application<Record<string, any>>'. Type '(req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>>) => Response<...>' is missing the following properties from type 'Application<Record<string, any>>': init, defaultConfiguration, engine, set, and 63 more.
路由代码(routes/index.ts)
import express, {Request, Response} from "express"; const v1Router = express.Router(); v1Router.get("/ping", (req : Request, res: Response)=>{ return res.json({ msg:"hello" }); }); export default v1Router;
错误固定出现在v1Router.get("/ping", (req : Request, res: Response)=>{这一行,已尝试多种解决途径(包括查阅相关技术帖子)均无效,且不想使用any类型规避问题。
解决方案
已定位问题根源:最新版本的@types/express存在类型定义兼容问题,将其降级至4.17.21版本后,服务即可正常运行。
内容的提问来源于stack exchange,提问作者Harshit
相关产品推荐
相关产品推荐

