Issue
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]
?
Solution
2023 update
The upcoming v6 will have another way to mark optional properties on type-level - the Opt
type, which does not suffer from the extension issues. You can use in two ways (and it won't :
import { Entity, PrimaryKey, Property, Opt } from '@mikro-orm/core';
@Entity()
export abstract class BaseEntity {
@PrimaryKey()
id: number;
@Property()
createdAt: Date & Opt = new Date(); // either intersect
}
@Entity()
export class EntityA extends BaseEntity<'isAnotherProperty'> {
@Property()
isAnotherProperty: Opt<boolean> = false; // or use it as a generic
}
This approach can be combined with the OptionalProps
symbol.
https://mikro-orm.io/docs/next/guide/relationships#alternative-approach-with-opt-type
Older (but still valid) approach
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<Optional = never> {
[OptionalProps]?: Optional | 'createdAt';
@PrimaryKey()
id: number;
@Property()
createdAt: Date = new Date();
}
@Entity()
export class EntityA extends BaseEntity<'isAnotherProperty'> {
@Property()
isAnotherProperty: boolean = false;
}
Answered By - Martin Adámek
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.