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

99
Views
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
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!