Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

102
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda