有没有办法使用mapped-types
withswagger
和class-transformer
这里有一个问题的例子: https ://stackblitz.com/edit/nestjs-starter-demo-aq1sw2?file=src/dtos/user-descriptor.dto.ts
DTO:
项目描述符Dto
// import { PickType } from '@nestjs/mapped-types';
import { PickType } from '@nestjs/swagger';
import { ProjectDto } from './project.dto';
export class ProjectDescriptorDto extends PickType(ProjectDto, [
'id',
'title',
] as const) {}
项目Dto
import { Expose, Type } from 'class-transformer';
import { UserDescriptorDto } from './user-descriptor.dto';
export class ProjectDto {
@Expose()
id: number;
@Expose()
description: string;
@Expose()
@Type(() => UserDescriptorDto)
starredBy: UserDescriptorDto[];
@Expose()
title: string;
}
用户描述符Dto
// import { PickType } from '@nestjs/mapped-types';
import { PickType } from '@nestjs/swagger';
import { UserDto } from './user.dto';
export class UserDescriptorDto extends PickType(UserDto, [
'id',
'firstName',
'lastName',
'email',
] as const) {}
用户Dto
import { ProjectDescriptorDto } from './project-descriptor.dto';
import { Type } from 'class-transformer';
import { ApiProperty } from '@nestjs/swagger';
export class UserDto {
id: number;
firstName: string;
lastName: string;
email: string;
@ApiProperty({
isArray: true,
type: ProjectDescriptorDto,
})
@Type(() => ProjectDescriptorDto)
favourites: ProjectDescriptorDto[];
}
因此,在示例中使用PickType
from@nestjs/mapped-types
编译代码,但它不会为扩展类生成正确的招摇规范
需要使用PickType
from@nestjs/swagger
才能正确生成 swagger 规范,但结合包中的@Type()
装饰器class-transform
,代码将无法正确编译:
TypeError: Cannot read properties of undefined (reading 'prototype')
at Object.PickType (/home/projects/nestjs-starter-demo-aq1sw2/node_modules/@nestjs/swagger/dist/type-helpers/pick-type.helper.js:13:38)
at Object.eval (/home/projects/nestjs-starter-demo-aq1sw2/dist/dtos/project-descriptor.dto.js:7:46)
at Object.function (https://nestjs-starter-demo-aq1sw2.jw.staticblitz.com/blitz.88a7151d177878d00bf438b30e057ed5805fdcd2.js:11:119417)
at Module._compile (https://nestjs-starter-demo-aq1sw2.jw.staticblitz.com/blitz.88a7151d177878d00bf438b30e057ed5805fdcd2.js:6:167880)
at Object.Module._extensions..js (https://nestjs-starter-demo-aq1sw2.jw.staticblitz.com/blitz.88a7151d177878d00bf438b30e057ed5805fdcd2.js:6:168239)
at Module.load (https://nestjs-starter-demo-aq1sw2.jw.staticblitz.com/blitz.88a7151d177878d00bf438b30e057ed5805fdcd2.js:6:166317)
at Function.Module._load (https://nestjs-starter-demo-aq1sw2.jw.staticblitz.com/blitz.88a7151d177878d00bf438b30e057ed5805fdcd2.js:6:163857)
at Module.require (https://nestjs-starter-demo-aq1sw2.jw.staticblitz.com/blitz.88a7151d177878d00bf438b30e057ed5805fdcd2.js:6:166635)
at i (https://nestjs-starter-demo-aq1sw2.jw.staticblitz.com/blitz.88a7151d177878d00bf438b30e057ed5805fdcd2.js:6:435073)
at _0x4139bb (https://nestjs-starter-demo-aq1sw2.jw.staticblitz.com/blitz.88a7151d177878d00bf438b30e057ed5805fdcd2.js:11:119029)
所以问题是;有没有办法解决这个问题,mapped-types
或者我是否不必扩展描述符。当前的解决方法是使用:
SomeDescriptorDto implements Pick<SomeDto, 'id' | ...> {
id: number;
// variables
}