我希望我的B 类能够扩展A 类。这按预期工作,但现在我需要重写 B 类中 A 类的属性。
具体来说,我必须将 A 类的属性设置为 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.
不幸的是,目前似乎没有办法通过继承来做到这一点:(
关于node.js - NestJS Mongoose 扩展架构并覆盖父级的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71172139/