Decorators
- Modify different
properties
,methods
and accessors
in a class
- Not equal to JS decorators
- Inside classes ONLY
"experimentalDecorators": true
and "emitDecoratorMetadata": true
must be enabled in tsconfig.json
- It is executed only ONE time when class is created and not when it is instantiated
function logError(target: any, key: string, desc: PropertyDescriptor): void {
console.log(target); // Prototype of the class (properties won't appear because they are created in the constructor). Prototype only stores methods definitions
console.log(key); // key of the class: method, property or accessor
console.log(desc); // property descriptions (Object.getOwnPropertyDescriptor)
}
- When a decorator is wrapper inside of a function it's called a Decorator factory
- It allows additional arguments to be passed