NestJS/ExpressJS 中的打字稿冲突类型枚举编号和编号

Typescript conflict type enum number and number in NestJS/ExpressJS

我会向我的 NestJS 服务器添加类型。

我写了一个控制器(为 Express 爱好者准备的路由)然后尝试指定参数的类型:

public async getAllMessages(
  @Query('startDate', ValidateDate) startDate: string,
  @Query('endDate', ValidateDate) endDate: string,
  @Res() res: Response,
): Promise<string> {
  const data = await this.crudService.getPeriodicMessages(startDate, endDate);
  return res.status(HttpStatus.OK).send(data);
}

这里我将类型 Response 添加到 res,因为我在 res.status(HttpStatus.OK).send(data).

中收到此错误消息

错误 : Cannot invoke an expression whose type lacks a call signature. Type 'Number' has no compatible call signatures..

我已经检查过 Response 类型有 status 类型 number 的成员并且 HttpStatus.OK 也是 enum number 类型的成员,那么我如何将它转换为 number?

我也阅读了,但无法解决我的问题。

在此示例中,无需使用 @Res() 注入响应对象。

您只需执行以下操作,nest 将自动处理响应。默认情况下,http 状态代码始终为 200(POST 为 201)。

public async getAllMessages(@Query('startDate', ValidateDate) startDate: string,
                            @Query('endDate', ValidateDate) endDate: string): Promise<string> {
  return this.crudService.getPeriodicMessages(startDate, endDate);
}

只有在特殊情况下才需要注入响应对象,比如动态设置响应码。