如何在Nestjs中扩展多个dto类

the*_*ire 3 javascript node.js typescript nestjs

我是新手nest.js,对此有疑问。

我想将多个类扩展Dto到我的主dto类,但我知道不可能扩展超过 2 个dto类。你知道该怎么做吗?

这是我的主要dto课程:

export class CarDto extends PickupLocationDto {
  @ApiProperty({ example: 'Aventador', description: 'The car name' })
  readonly modelName: string;
}
Run Code Online (Sandbox Code Playgroud)

最近我只能从PickupLocationDto类中扩展它,但我想再扩展一个dto类到这个CarDto类。

任何帮助表示赞赏。

the*_*ire 9

由于我使用 swagger,因此使用@nestjs/mapped-typespackage 不会显示相交 dto 中的所有变量。因此,我使用 swagger 中的 IntersectionType

import { ApiProperty, IntersectionType } from '@nestjs/swagger';

export class Dto3 extends IntersectionType(
  Dto1,
  Dto2,
) {}
Run Code Online (Sandbox Code Playgroud)