How to extend more than one dto class in Nestjs

Issue

I am new to nest.js and have a question about it.

I want to extend more than one Dto to my main dto class, but I know it is not possible to extend more than 2 dto classes. Do you have any idea how to do it ?

Here is my main dto class:

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

Recently I am only able to extend it from PickupLocationDto class, but I want to extend one more dto class to this CarDto class.

Any help is appreciated.

Solution

Since I use swagger, using @nestjs/mapped-types package does not show all the variables from intersected dtos. therefore, I use IntersectionType from swagger

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

export class Dto3 extends IntersectionType(
  Dto1,
  Dto2,
) {}

Answered By – theboyonfire

Answer Checked By – David Marino (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.