简体   繁体   中英

How to add and declare new property to class using decorator in typescript (angular)

I'm trying to add new property to class using decorator. Here is my code

function classDecorator<T extends {new(...args: any[]): {}}>(constructor: T) {
    constructor.prototype.newProperty = 'some value';
}


@classDecorator
class MyClass {
  property1: string = 'value1';
  property2: string = 'value1';
}
const obj = new MyClass();

console.log(obj);
console.log(obj.property1);
console.log(obj.property2);
console.log(obj.newProperty); //  error TS2551: Property 'newProperty' does not exist on type 'MyClass'

Is there any way to make angular to understand that MyClass also have newProperty ? Can I do it with .d.ts file?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM