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?
Thanks!
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.
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.
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.
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.