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

100
Visualizações
change the class definition with TS decorator

I'm trying to reproduce the @Getters of the common Java library Lombok. The result would be :

@Getters()
export class Example {
  private property: string;
}
const test = new Example();
console.log(test.getProperty());

To do this, I try to change the js class definition by override it with a TS Decorator:

import { capitalize } from "lodash";

export const Getters = () => <T extends {new(...args:any[]):{}}>(constructor:T) => {  
  return class extends constructor {
    constructor(...args: any[]) {
      super(...args);
      const props = Reflect.ownKeys(this);
      props.forEach((prop: string) => {
        const capitalizedKey = capitalize(prop);
        const methodName = `get${capitalizedKey}`;
        Reflect.defineProperty(this, methodName, { value: () => this[prop], configurable: false, enumerable: false });
      });
    }
  }
}

For the moment, I have this error The X property does not exist on the X type.

But I can have the result with this:

console.log(test['getProperty']())

Doe's someone know how to change the class definition with TS decorator pls?

Playground: https://typescript-lodash-playground-1fuxpz.stackblitz.io

about 4 years ago · Santiago Gelvez
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