类型脚本:类返回号,而不是对象实例

时间:2020-01-20 10:59:58

标签: typescript typeorm

大家好,我正在使用Typescript,Nest Js和TypeORm

我正在尝试对参数进行类验证:

import { IsNumber, IsPositive } from 'class-validator';
import { Transform } from 'class-transformer';

export class validateID {
    @Transform((id) => parseInt(id, 10))
    @IsNumber()
    @IsPositive()
    id: number;
}

但是当我在Route控制器中指定它时,我说我应该返回typeOf validateID,但是我想返回数字,因为该数字已经过验证并且应该返回

  @Get(':id')
    @HttpCode(HttpStatus.OK)
    async getUser(@Param('id') id: validateID): Promise<CrudResponse<User>> {
        data: await this.userService.getUser(id), // here it asks for type to be validateID, but I would like it to be number

    }

1 个答案:

答案 0 :(得分:0)

确定,我设法在路线中返回了以下数字:

validateID['id']

所以路由中的代码如下所示:

@Get(':id')
    @HttpCode(HttpStatus.OK)
    async getUser(@Param('id') id: validateID['id']): Promise<CrudResponse<User>> {
        data: await this.userService.getUser(id), // here it asks for type to be validateID, but I would like it to be number

    }