DEV Community

loading...
Cover image for TS decorators (1/2): the basics

TS decorators (1/2): the basics

pinutz23 profile image Jannik Wempe Updated on ・5 min read

Introduction

This is the first post of a series related to TypeScript decorators.

This post should answer the following questions:
⁉️ What are decorators? What types of decorators are there?
⁉️ How can they be used?
⁉️ When are they executed?

Later posts will show implementations for each of the decorator types and provide some use cases.

Decorators

Decorators are a stage 2 ECMAScript proposal ("draft"; purpose: "Precisely describe the syntax and semantics using formal spec language."). Therefore, the feature isn't included in the ECMAScript standard yet. TypeScript (early) adopted the feature of decorators as an experimental feature.

But what are they? In the ECMAScript proposal they are described as follows:

Decorators @decorator are functions called on class elements or other JavaScript syntax forms during definition, potentially wrapping or replacing them with a new value returned by the decorator.

In the TypeScript handbook decorators are described as:

Decorators provide a way to add both annotations and a meta-programming syntax for class declarations and members.

To put it in a more general way: you can change the behaviour of certain parts of the code by annotating them with a decorator. The parts of the code, which can be annotated with decorators are described in the Types of decorators section.

BONUS: There is even a decorator pattern described in the Design Patterns book by the Gang of Four. Its intent is described as:

to add responsibilities to individual objects dynamically and transparently, that is, without effecting other objects.

Enable decorators

Since decorators are an experimental feature, they are disabled by default. You must enable them by either enabling it in the tsconfig.json or passing it to the TypeScript compiler (tsc). You should also at least use ES5 as a target (default is ES3).

tsconfig.json

{
  "compilerOptions": {
    "target": "ES5",
    "experimentalDecorators": true
  }
}
Enter fullscreen mode Exit fullscreen mode

CLI

tsc -t ES5 --experimentalDecorators
Enter fullscreen mode Exit fullscreen mode

You might also want to have a look at the related Emit Decorator Metadata setting (which is not in scope of this post.)

Types of decorators

There are 5 different types of decorators:

  • class decorators
  • property decorators
  • method decorators
  • accessor decorators (== method decorator applied to getter / setter function)
  • parameter decorators

The following example shows where they can be applied:

// this is no runnable code since the decorators are not defined

@classDecorator
class Polygon {
  @propertyDecorator
  edges: number;
  private _x: number;

  constructor(@parameterDecorator edges: number, x: number) {
    this.edges = edges;
    this._x = x;
  }

  @accessorDecorator
  get x() {
    return this._x;
  }

  @methodDecorator
  calcuateArea(): number {
    // ...
  }
}
Enter fullscreen mode Exit fullscreen mode

Class constructors can not have a decorator applied.

Overview over signatures

Each of the decorator functions receives different parameters. The accessor decorator is an exception, because it is essentially just a method decorator, which is applied to an accessor (getter or setter).

The different signatures are defined in node_modules/typescript/lib/lib.es5.d.ts:

interface TypedPropertyDescriptor<T> {
  enumerable?: boolean;
  configurable?: boolean;
  writable?: boolean;
  value?: T;
  get?: () => T;
  set?: (value: T) => void;
}

declare type ClassDecorator = <TFunction extends Function>(target: TFunction) => TFunction | void;
// also applies for accessor decorators
declare type PropertyDecorator = (target: Object, propertyKey: string | symbol) => void;
declare type MethodDecorator = <T>(target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T> | void;
declare type ParameterDecorator = (target: Object, propertyKey: string | symbol, parameterIndex: number) => void;
Enter fullscreen mode Exit fullscreen mode

Order of evaluation

The different types of decorators are evaluated in the following order:
⬇️ instance members: Property Decorators first and after that Accessor, Parameter or Method Decorators
⬇️ static members: Property Decorators first and after that Accessor, Parameter or Method Decorators
⬇️ Parameter Decorators are applied for the constructor.
⬇️ Class Decorators are applied for the class.

Bringing the different types, their signatures and order of evaluation together:


function propertyDecorator(target: Object, propertyKey: string | symbol) {
  console.log("propertyDecorator", propertyKey);
}
function parameterDecorator(target: Object, propertyKey: string | symbol, parameterIndex: number) {
  console.log("parameterDecorator", propertyKey, parameterIndex);
}
function methodDecorator<T>(target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>) {
  console.log("methodDecorator", propertyKey);
}
function accessorDecorator<T>(target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>) {
  console.log("accessorDecorator", propertyKey);
}
function classDecorator(target: Function) {
  console.log("classDecorator");
}

@classDecorator
class Polygon {
  @propertyDecorator
  private static _PI: number = 3.14;
  @propertyDecorator
  edges: number;
  private _x: number;

  constructor(@parameterDecorator edges: number, x: number) {
    this.edges = edges;
    this._x = x;
  }

  @methodDecorator
  static print(@parameterDecorator foo: string): void {
    // ...
  }

  @accessorDecorator
  static get PI(): number {
    return Polygon._PI;
  }

  @accessorDecorator
  get x() {
    return this._x;
  }

  @methodDecorator
  calcuateArea(@parameterDecorator bar: string): number {
    return this.x * 2;
  }
}

console.log("instantiating...")
new Polygon(3, 2)

// Output:
//   [LOG]: "propertyDecorator",  "edges"
//   [LOG]: "accessorDecorator",  "x"
//   [LOG]: "parameterDecorator",  "calcuateArea",  0
//   [LOG]: "methodDecorator",  "calcuateArea"
//   [LOG]: "propertyDecorator",  "_PI"
//   [LOG]: "parameterDecorator",  "print",  0
//   [LOG]: "methodDecorator",  "print"
//   [LOG]: "accessorDecorator",  "PI"
//   [LOG]: "parameterDecorator",  undefined,  0
//   [LOG]: "classDecorator"
//   [LOG]: "instantiating..."
Enter fullscreen mode Exit fullscreen mode

Open example in Playground

Decorator factories

Maybe you already asked yourself, after having a look at the different signatures, how to pass additional properties to the decorator functions. The answer to that is: with decorator factories.

Decorator factories are just functions wrapped around the decorator function itself. With that in place, you are able to pass parameters to the outer function in order to modify the behavior of the decorator.

Example:

function log(textToLog: string) {
  return function (target: Object, propertyKey: string | symbol) {
    console.log(textToLog);
  }
}

class C {
  @log("this will be logged")
  x: number;
}

// Output:
//   [LOG]: "this will be logged"
Enter fullscreen mode Exit fullscreen mode

Open example in Playground:

I know this example isn't too exciting, but it opens the door for a lot of possibilities. But I will keep some of them for the following parts of this series 😉

Decorator composition

Can you apply multiple decorators at once? Yes! In what order are they executed? Have a look:

function log(textToLog: string) {
  console.log(`outer: ${textToLog}`)
  return function (target: Object, propertyKey: string | symbol) {
    console.log(`inner: ${textToLog}`)
  }
}

class C {
  @log("first")
  @log("second")
  x: number;
}

// Output:
//   [LOG]: "outer: first"
//   [LOG]: "outer: second"
//   [LOG]: "inner: second"
//   [LOG]: "inner: first"
Enter fullscreen mode Exit fullscreen mode

Open example in Playground

The decorator factories are executed in the order of their occurrence and the decorator functions are executed in reversed order.

Resources

🔗 TypeScript Handbook - Decorators
🔗 GitHub issue discussion about adding Decorators to TypeScript

ECMAScript proposals

🔗 ECMAScript proposals
🔗 ECMAScript decorator proposal

Wrap Up

The first part of this series addressing TypeScript decorators was about the basics. By now you should now what decorators are, how they look and how they are executed. In the next parts I'd like to provide some examples of more useful decorators for each type.

Feedback welcome

I'd really appreciate your feedback. What did you (not) like? Why? Please let me know, so I can improve the content.

I also try to create valuable content on Twitter: @pinutz23.

Discussion (2)

pic
Editor guide
Collapse
tomy profile image
Tomy Smith

Very nice! Have you found the need to create your own decorators yet? I know popular projects such as Angular and Inversify use them, but I've never found the need to reach for them to create functionality myself.

Collapse
pinutz23 profile image
Jannik Wempe Author

Hi Tomy,
I am happy that you like it! 😊
Most of the frameworks use it for dependency injection (DI). That is also what Inversify is created for. I will write something about DI as well...

Most of the time I have used them for debugging purposes. For example log execution time or log value changes.

But one goal for me writing these posts is to deal with decorators in depth and find valuable use case and write them down 😉