Decorators

TypeScript decorators are based on the ES7 decorator proposal.

A decorator is:

  • an expression
  • that evaluates to a function
  • that takes the target, name, and property descriptor as arguments
  • and optionally returns a property descriptor to install on the target object

For more information, please see the Decorators proposal.

Example

Decorators readonly and enumerable(false) will be applied to the property method before it is installed on class C. This allows the decorator to change the implementation, and in this case, augment the descriptor to be writable: false and enumerable: false.

  1. class C {
  2. @readonly
  3. @enumerable(false)
  4. method() { }
  5. }
  6. function readonly(target, key, descriptor) {
  7. descriptor.writable = false;
  8. }
  9. function enumerable(value) {
  10. return function (target, key, descriptor) {
  11. descriptor.enumerable = value;
  12. }
  13. }