Property Decorators
A property decorator is a decorator function that is applied to a single property of a class. Unlike in a method or class decorators, you cannot modify or replace the property definition, but you can indeed observe it.
Note
Since you receive the constructor function in the decorator, this is not strictly true. You could change the code of the class, but it's extremely inadvisable.
When a property decorator is called, it receives two parameters: target
and propertyKey
:
target
: Since properties can be both instance properties (defined on instances of the class) and static properties (defined on the class itself),target
can be two different things. For instance properties, it's the prototype of the class. For static properties, it's the constructor function of the class. Usually, you would type this parameter asany
.propertyKey
: This is the name of the property you're decorating.
In contrast to the method decorators...