0

I have two classes.

UserDto

export class UserDto {
  name: string;
}

AuthDto

export class AuthDto {
    email: string;
    password: string;
}

I want to create a class of RegistrationDto, which will be a merge of these two classes.

I tried to do this

export class RegisterDto extends UserDto, AuthDto {}

But this only works for interfaces. I am using it as Nestjs dtos, so if I'm thinking about it in the wrong way, please let me know.

How do I handle the classes in this case?

Alex Ironside
  • 4,658
  • 11
  • 59
  • 119
  • Where are these classes coming from? Why don't you turn them into interfaces? –  Jun 04 '21 at 11:04
  • Similar to a question I called this morning. I got the solution from here. https://stackoverflow.com/a/26961710/8015757. Also, I asked a similar question a while ago. I think everyone is having the same problem today. lol – Onur Özkır Jun 04 '21 at 11:07
  • Huh. Interesting. @ChrisG Huh. Good point. Didn't know Nest accepts interfaces. Thanks! You can add it as answer so I can select as best if you'd like. Unless there is some drawbacks for this approach – Alex Ironside Jun 04 '21 at 11:19

3 Answers3

2

You can extend AuthDto to UserDto or vice-versa:

export class AuthDto extends UserDto {
    email: string;
    password: string;
}

And then do this:

export class RegisterDto extends AuthDto {}

Just a suggestion

Maverick Fabroa
  • 1,105
  • 1
  • 9
  • 15
1

Why don't you consolidate them to a new class:

export class RegisterDto{
    authDto: AuthDto;
    userDto: UserDto;
}
0

You can combine both the DTO's into one using the Intersection Type

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

export class RegisterDto extends IntersectionType(
  AuthDto,
  UserDto,
) {}

Reference: https://docs.nestjs.com/openapi/mapped-types#intersection

Istiyak Tailor
  • 1,570
  • 3
  • 14
  • 29