Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

242
Views
OptionalProps in Mikro-orm

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]?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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;

}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!