I'm struggling with creating a TypeScript decorator that modifies the get method for a property within a class. The issue I'm facing is getting it to affect instances of the class.
Below is an example scenario:
function CustomDecorator() {
return function (target: Object, propertyKey: string) {
Object.defineProperty(target, propertyKey, {
get: function () {
return 42;
},
});
};
}
export class ExampleClass {
@CustomDecorator()
exampleProperty = 0;
}
const instance = new ExampleClass();
console.log(instance.exampleProperty);// Expected output is 42, but currently returns 0
If anyone could offer guidance or provide a solution, I would greatly appreciate it.
I've attempted replacing the target constructor with a new one that executes Object.defineProperty, however, this approach failed as well.