Decorator factory
In the previous example, we saw how we can create a simple decorator and annotate that on a class. But if this was the only thing decorators did in TypeScript then they would be very restrictive. In real-world applications, you would want to have the flexibility to pass some custom values to the decorator from the class or a function. This is where the decorator factory comes in.
The decorator factory allows us to define additional parameters that can then be used to pass values from the class to the decorator. Let's refactor the logger decorator we defined earlier to pass a parameter to it. The first step is to create a function similar to what we created earlier for the logger, but now have the parameters of the function ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access