I am trying to work out how to define additional optional properties.
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;
}
With the above TypeScript throws an error:
Property '[OptionalProps]' in type 'EntityA' is not assignable to the same property in base type 'BaseEntity'.
Basically my BaseEntity has optional properties, as does EntityA. I could remove [OptionalProps]?: from BaseEntity and have [OptionalProps]?: 'createdAt' | 'isAnotherProperty'; in EntityA, but many of my entities don't require any additional optional properties beyond createdAt so I prefer not to have to duplicate [OptionalProps]?: 'createdAt'; in every entity class if I could just 'extend' it where I need to.
Is it at all possible to either append to or override [OptionalProps]?
CodePudding user response:
Probably the cleanest approach is via type argument on the base entity:
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;
}
