Typescript class decorator access created property

Hello there, I have a question about the typescript class decorator.
From the course video, I know that I could set new prototype properties like this:

But I have no idea how I could access the properties that I had created for that class (Pizza)
The message said that ‘property sauce does not exist on type pizza’.
How could I access the ‘sauce’ property here?

Did you enable decorators in tsconfig? I think it is an experimental feature you have to enable manually. At least that is what I recall from when I did the course.

That documentation says you need to enable the feature and target a high enough version of JavaScript (at least ES5).

Yes, I do enable the experimentalDecorator setting to true, and the target is set to “ES2016”.
It works well with method, accessor, parameter, property decorators but class decorator.
Note that the decorator function was executed since I could get the console.log message from it. The only problem I have is not accessing the added prototype properties.

Apparently this is working as intended:

Note that the decorator does not change the TypeScript type and so the new property reportingURL is not known to the type system

It does not say how you should access the property, but I wonder if this would work:


I still cannot access the sauce property using “pizza[‘sauce’]”
I tried to add a property sauce inside the class and initialize it with a random string value.
After that, console.log(pizza.sauce) did not emit error anymore, but I still could not get the value that was given inside the decorator.

My code is as follows:

function Sauce(name: string) {
return (constructor: Function) => {
constructor.prototype.sauce = name;

class Pizza {
sauce: string = “asd”;

let pizza = new Pizza();
// this will print “asd” and not “pesto”

Maybe we need to try a simple diagnostic: try logging the objects on the console and see if the fields are actually being added. I also found another technique we could try here:

It involves returning a subclass of the supplied class constructor.