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.
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.
Share Improve this question edited Dec 23, 2020 at 7:22 namgold 1,0701 gold badge11 silver badges34 bronze badges asked Dec 23, 2020 at 6:51 theboyonfiretheboyonfire 5952 gold badges8 silver badges26 bronze badges 1-
2
do remember
ValidationPipe
will not work with the entended classes properties. – arizafar Commented Dec 23, 2020 at 8:26
3 Answers
Reset to default 11Since 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,
) {}
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,
) {}
You can also use the extend keyword to extend the DTO class
export class Dto3 extends Dto1 {
public readonly dtoField: Dto2;
}