NestJS Mongoose 扩展架构并覆盖父级的 属性

NestJS Mongoose extend Schema and override property of parent

我想要我的 Class B 扩展一个 Class A。这按预期工作,但现在我需要在 Class B.

中覆盖 Class A 的 属性

具体来说,我必须将 Class 中的 属性 设为 Class B 的可选项:

export class B extends A {
  // This property is available in Class A
  @Prop({ required: false, index: true })
  @ApiProperty()
  propertyToOverride: number;
}

我在 NestJS 的 Discord 上问过这个问题,这是我从版主 Scott (Scott (EN-DE)#0052):

There is no overriding of prop properties. And, the classic inheritance of schema definitions doesn't work either, i.e. with overriding. If you are extending from a schema class, you can only add additional fields to it in your new class. So, it is a reusability tool and not a polymorphism tool. If you need polymorphism, then you need to look at discriminators.

所以不幸的是,目前似乎没有办法通过继承来做到这一点:(