From the course: TypeScript Essential Training

Unlock the full course today

Join today to access over 23,400 courses taught by industry experts.

Creating a class decorator

Creating a class decorator

- [Instructor] Now that I've shown the fundamentals of how decorators and decorator factories work with methods, I'll show you how to apply decorators to classes. At a high level, the approach is pretty much the same. For example, if I wanted to create a decorator to freeze a class to prevent any changes, I can create a function that accepts a single parameter with the function type like this. (keyboard keys tapping) And we apply the decorator to the class by placing it just before the class definition like this. So far, so good. It is the same familiar syntax that is used to create method decorators. The biggest difference, however, is what is actually being decorated. In other words, what exactly is the target that this parameter references? As the function type implies, the target being passed to the class decorator is not the object being created when the class is instantiated. Instead, it's the class's constructor…

Contents