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

0 votes

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?

Jul 5 in TypeSript by Logan
• 2,140 points
12 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In TypeSript

0 votes
1 answer

How to declare and initialize a Dictionary in Typescript

Apparently this doesn't work when passing the ...READ MORE

answered May 31 in TypeSript by Nina
• 3,020 points
223 views
0 votes
1 answer

How to use moment.js library in angular 2 typescript app?

REFERENCE: https://momentjs.com/docs/#/use-it/typescript/ You can install this by using: npm install ...READ MORE

answered Jun 1 in TypeSript by Nina
• 3,020 points
63 views
0 votes
1 answer

How to install and run Typescript locally in npm?

It took me a while to figure ...READ MORE

answered Jun 8 in TypeSript by Nina
• 3,020 points
206 views
0 votes
1 answer

how to use spread syntax and new Set() with TypeScript

The ... operator should work on anything ...READ MORE

answered Jun 10 in TypeSript by Nina
• 3,020 points
12 views
0 votes
1 answer
0 votes
1 answer

How to create ES6 Map in Typescript

Refer this as an example this.configs = new ...READ MORE

answered May 31 in TypeSript by Nina
• 3,020 points
24 views
0 votes
1 answer

How to apply zoom animation for each element of a list in angular?

Hey @Sid, do check if this link ...READ MORE

answered Jul 30, 2019 in Others by Vardhan
• 13,200 points
512 views
0 votes
1 answer

Can't bind to 'formGroup' since it isn't a known property of 'form'

In order to rectify this error, you ...READ MORE

answered Feb 10 in Others by Rahul
• 9,000 points
1,282 views
0 votes
1 answer

How to set meta tags using Angular universal SSR and ngx-seo plug-in?

first Install the plug-in with npm i ngx-seo ...READ MORE

answered Feb 11 in Others by narikkadan
• 11,280 points
728 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP