Is it possible to add an additional property using a property decorator?
The current approach I am taking seems to be incorrect.
const RoProp = () => {
return <T>(target: T, memberName: keyof T) => {
const roPropName = `${String(memberName)}_ro`;
let roPropVal = false;
Object.defineProperty(target, roPropName, {
set(v: boolean) {
roPropVal = v;
},
get(): boolean {
return roPropVal;
},
enumerable: true,
});
};
};
class ExampleClass {
@RoProp() testProp: string;
// Property 'testProp_ro' does not exist on type 'ExampleClass'.(2339)
constructor({ testProp, testProp_ro }: ExampleClass) {
this.testProp = testProp;
this.testProp_ro = testProp_ro; // Property 'testProp_ro' does not exist on type 'ExampleClass'.(2339)
}
}
const exampleInst = new ExampleClass({
testProp: "a test",
testProp_ro: false, // Argument of type '{ testProp: string; testProp_ro: boolean; }' is not assignable to parameter of type 'ExampleClass'. Object literal may only specify known properties, and 'testProp_ro' does not exist in type 'ExampleClass'.(2345)
});
exampleInst.testProp_ro = true; // Property 'testProp_ro' does not exist on type 'ExampleClass'.(2339)
Playground Link: Trying to create property with decorator
What is the correct way to achieve this task, if possible at all?