我正在尝试如何定义其他可选属性。
import { Entity, PrimaryKey, Property, OptionalProps } from '@mikro-orm/core';
@Entity()
export abstract class BaseEntity {
[OptionalProps]?: 'createdAt';
@PrimaryKey()
id: number;
@Property()
createdAt: Date = new Date();
}
@Entity()
export class EntityA extends BaseEntity {
[OptionalProps]?: 'isAnotherProperty'; // This is the bit I cannot figure out
@Property()
isAnotherProperty: boolean = false;
}使用上面的TypeScript引发一个错误:
Property '[OptionalProps]' in type 'EntityA' is not assignable to the same property in base type 'BaseEntity'.基本上,我的BaseEntity具有可选属性,EntityA也是如此。我可以从[OptionalProps]?:中删除BaseEntity,并在EntityA中使用[OptionalProps]?: 'createdAt' | 'isAnotherProperty';,但是我的许多实体不需要createdAt以外的任何额外的可选属性,所以如果我可以在需要的地方‘扩展’,那么我不希望在每个实体类中复制[OptionalProps]?: 'createdAt';。
是否有可能追加或覆盖[OptionalProps]
发布于 2022-02-07 08:16:52
最干净的方法可能是通过基实体上的type参数:
import { Entity, PrimaryKey, Property, OptionalProps } from '@mikro-orm/core';
@Entity()
export abstract class BaseEntity<O> {
[OptionalProps]?: O | 'createdAt';
@PrimaryKey()
id: number;
@Property()
createdAt: Date = new Date();
}
@Entity()
export class EntityA extends BaseEntity<'isAnotherProperty'> {
@Property()
isAnotherProperty: boolean = false;
}https://stackoverflow.com/questions/71013801
复制相似问题