我试图使用TypeScript在Express中创建一个路由,所以我像往常一样,将createUser
函数的参数设置为req
:Request
,res
:Response
和next
:NextFunction
,显然是从@types/express
开始的。每次都能工作,但这次它显示错误:
No overload matches this call.
Overload 1 of 3, '(path: PathParams, ...handlers: RequestHandler<ParamsDictionary, any, any, ParsedQs>[]): Router', gave the following error.
Argument of type '(req: Request, res: Response, next: any) => Promise<any>' is not assignable to parameter of type 'RequestHandler<ParamsDictionary, any, any, ParsedQs>'.
Types of parameters 'req' and 'req' are incompatible.
Type 'Request<ParamsDictionary, any, any, ParsedQs>' is missing the following properties from type 'Request': cache, credentials, destination, integrity, and 15 more.
Overload 2 of 3, '(path: PathParams, ...handlers: RequestHandlerParams<ParamsDictionary, any, any, ParsedQs>[]): Router', gave the following error.
Argument of type '(req: Request, res: Response, next: any) => Promise<any>' is not assignable to parameter of type 'RequestHandlerParams<ParamsDictionary, any, any, ParsedQs>'.
Type '(req: Request, res: Response, next: any) => Promise<any>' is not assignable to type 'RequestHandler<ParamsDictionary, any, any, ParsedQs>'.
Overload 3 of 3, '(path: PathParams, subApplication: Application): Router', gave the following error.
Argument of type '(req: Request, res: Response, next: any) => Promise<any>' is not assignable to parameter of type 'Application'.
Type '(req: Request, res: Response, next: any) => Promise<any>' is missing the following properties from type 'Application': init, defaultConfiguration, engine, set, and 61 more.
字符串
如何解决这个问题?下面是应用程序的一些代码:
protected setupRoutes(): void {
this.router.post("/", this.createUser);
}
// Routes:
private async createUser(req: Request, res: Response, next): Promise<any> {
try {
res.status(200).send("Hi!");
} catch (err) {
return next({
status: err.status,
message: err.message,
});
}
}
型
2条答案
按热度按时间piv4azn71#
我很惊讶这对你有用。Express有自己的类型可用于请求和响应,它们扩展了你正在使用的基本类型。
例如,下面是type definition of the express request:
字符串
尝试使用这些类型:
型
js4nwp542#
我使用
express.Request
和express.Response
来解决这个问题字符串