Decorators and Namespaces in TypeScript
What are Decorators?
Decorators in TypeScript are a special function that can be attached to a class, method, accessor, property, or parameter. Decorators provide a way to add metadata, modify behavior, or perform additional initializations on the programming entities to which they are applied.
To better understand decorators, let's imagine we want to log the execution time of a method. We can create a decorator that does this for us.
Simple example of a decorator:
typescript
In this example, the logExecutionTime
decorator attaches to the someMethod
method of the Example
class. Every time someMethod
is called, the decorator logs the time it takes to execute that method.
Decorators can be applied to different elements of a class, such as properties and methods, and can be reused in various parts of the code.
Types of Decorators:
- Class Decorators: Applied to entire classes.
- Property Decorators: Applied to properties within a class.
- Method Decorators: Applied to methods within a class.
- Parameter Decorators: Applied to parameters of a method within a class.
Throughout this course, we will explore each of these types in detail with practical examples.
Support Chuck’s Academy!
Enjoying this course? I put a lot of effort into making programming education free and accessible. If you found this helpful, consider buying me a coffee to support future lessons. Every contribution helps keep this academy running! ☕🚀

Chat with Chuck
