You need to add the property to the prototype and not the constructor:
function MyClassDecorator(target: any): any {
target.prototype.myNickname = "Gambler";
}
That will get you what you want, but the problem is that you won't be able to access this property without typescript complaining:
let myClass = new MyClass();
console.log(myClass.myNickname); // error: Property 'myNickname' does not exist on type 'MyClass'
You can try something like:
function myClassFactory(): MyClass & { myNickname: string } {
return new MyClass() as MyClass & { myNickname: string };
}
let myClass = myClassFactory();
console.log(myClass.myNickname); // all good
(code in playground)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…