Reputation: 595
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.
Upvotes: 3
Views: 10093
Reputation: 69
You can also use the extend keyword to extend the DTO class
export class Dto3 extends Dto1 {
public readonly dtoField: Dto2;
}
Upvotes: 0
Reputation: 595
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,
) {}
Upvotes: 11
Reputation: 10824
You can use mapped-types
to do that, first you will need to install the package (yarn add @nestjs/mapped-types
) then use IntersectionType
just like this:
import { IntersectionType } from '@nestjs/mapped-types';
export class Dto3 extends IntersectionType(
Dto1,
Dto2,
) {}
Upvotes: 4