我有以下课程
export class DocumentsSteps {
@ApiProperty({type: ???})
[type: string]: DocumentStep;
}
我应该如何定义 ApiProperty 类型?
我有以下课程
export class DocumentsSteps {
@ApiProperty({type: ???})
[type: string]: DocumentStep;
}
我应该如何定义 ApiProperty 类型?
你可以用一个函数包装它
export type Constructor<I> = new (...args: any[]) => I
function ErrorDto(statusCode: number, message: string): Constructor<Error>{
class Error implements Error{
@ApiProperty({ example: statusCode })
readonly statusCode: number
@ApiProperty({ example: message })
readonly message: string
}
return Error
}
export class UnauthorizedErrorDto extends ErrorDto(401, 'Unauthorized'){}
export class BadRequestErrorDto extends ErrorDto(400, 'Bad Request'){}
看一下这个
https://docs.nestjs.com/custom-decorators#decorator-composition
您可以实现另一个装饰器来扩展 ApiProperty
export function CustomApiProperty(type: string) {
return applyDecorators(
ApiProperty({type, ...}),
);
}