Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

247
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda