nestjs/mongoose

IntersectionType and Prop decorator

Closed this issue · 2 comments

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

When I use IntersectionType:

export class First {
  @Prop()
  first: string;
}

export class Second {
  @Prop()
 second: string;
}

export class Last extends IntersectionType(First, Second) {
 @Prop()
 last: string;
}

The problem is that properties like first and second are ignored.

const result = await this.model.create({
  first: 'First',
  second: 'Second',
  last: 'Last'
});

output:

{last: 'Last'}

Minimum reproduction code

https://github.com/nestjs/mongoose

Steps to reproduce

No response

Expected behavior

const result = await this.model.create({
  first: 'First',
  second: 'Second',
  last: 'Last'
});

output:

{first: 'First', second: 'Second', last: 'Last'}

Package version

10.0.4

mongoose version

8.0.2

NestJS version

10.3.3

Node.js version

21.7

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

The classes from @nestjs/mapped-types only carry forward metadata declared by class-transformer and class-validator package based decorators. There is no mention in the docs that you can use it for @nestjs/mongoose class based schemas too.

I don't feel this is a bug. You'll just have to make amends to your code and follow a different hierarchal approach.

As @prateekkathal mentioned above, IntersectionType utility function isn't supposed to be used with ODMs/ORMs and that's by design