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