

Here, this is the function parameter “originalMethod”. Actually, decorators are simple functions that are passed the function (or class/parameter) to decorate.

Under the hood, a decorator looks something like the one in Listing 2 for the “logged” decorator. This is possible because the experimental decorators can still be activated like before.ĭecorators can be used to customize or extend the behavior of classes and their methods, set/get accessors, and properties.ĭecorators can be used for this. This leads to the fact that some well-known projects and libraries, like Angular or type-graphql, won’t switch to the new specification for the time being. So even in TypeScript itself, the new decorators differ from the previous ones. Unfortunately, this ECMAScript specification is slightly different from the previously usable decorators form in TypeScript, which is activated with “–experimentalDecorators”. In general, TypeScript 5 implements Stage 3 of the ECMAScript Decorators proposal.

To make this happen, there are open tickets in TypeScript. However, changing or restricting the types of method signatures or classes using decorators isn’t possible yet. You can also store initialization logic for the decorated field. Therefore, (method/class) name, visibilities, and arguments are also available. Implementing a decorator is based on an interceptor. This can come from a separate method call or even an HTTP API call. One example is enriching the return data with additional information. This can refer to both calling the method (a cache, for instance) and its return. In contrast to the proxy pattern, this isn’t about controlling access to a method, but extending the method’s behavior with added functionality.

To help you understand, imagine that the name “decorator” is derived from the classic “Gang-of-Four” decorator pattern. Generally, decorators are a way to implement repetitive aspects and apply them in multiple places.
