Decorators and Namespaces in TypeScript
Class Decorators
Class decorators are applied to complete classes and are used to modify or extend their behavior.
A class decorator is a function that applies to the constructor of a class and is often used for tasks such as registering classes, applying design patterns, or configuring additional metadata.
Syntax of a Class Decorator:
typescript
In this example, the classDecorator
decorator is applied to ExampleClass
. When the ExampleClass
is defined, the decorator executes and logs the name of the class to the console.
Practical Example:
Suppose we want to add a method to all classes that use a specific decorator.
typescript
In this example, the addTimestamp
decorator adds a timestamp
property to ExampleWithTimestamp
. Thus, each instance of ExampleWithTimestamp
will have a timestamp that can be logged using the logTimestamp
method.
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
