Decorators in TypeScript
Discover how decorators add metadata to your code and simplify development in frameworks like Angular.
Decorators in TypeScript is a free TypeScript Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
Introduction to Decorators
Decorators in TypeScript are special functions that allow you to add metadata or modify the behavior of classes, methods, and properties.
They are often used in frameworks like Angular to simplify development.

2
How Decorators Work
A decorator is a function that is applied to a class or its members (methods, properties, etc.).
For example, @Component in Angular is a decorator that adds metadata to a class to define it as a component.
3
Using Built-in Decorators
TypeScript has built-in decorators such as:
@readonly: Makes a property read-only.@enumerable: Controls whether a property is enumerable.
These decorators help modify the behavior of properties or methods directly.
4
Creating a Simple Decorator
You can create your own decorator to log messages or add functionality:
Apply it to a property: @Log.
function Log(target: any, propertyName: string) {
console.log(`Property ${propertyName} was accessed`);
}5
Class Decorators
Class decorators are used to modify or enhance the behavior of an entire class:
function SimpleDecorator(constructor: Function) {
console.log(`${constructor.name} is decorated`);
}
Apply it to a class: @SimpleDecorator.
function SimpleDecorator(constructor: Function) {
console.log(`${constructor.name} is decorated`);
}6
Method Decorators
Method decorators modify the behavior of a class method:
Use it on a method: @LogMethod.
function LogMethod(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
console.log(`Method ${propertyKey} was called`);
}7
Using Decorators in Angular
Angular heavily relies on decorators to add metadata to its components:
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent {}
The @Component decorator defines the class as an Angular component.
8
9
Best Practices for Using Decorators
When using decorators:
- Keep them simple and focused.
- Use them for metadata and not for complex logic.
- Leverage built-in decorators when possible to save time.
10
Congratulations!
You’ve learned how decorators work in TypeScript and how they can be used to modify or enhance your code. Keep experimenting with decorators to master their use in your projects!

Frequently asked questions
Is the “Decorators in TypeScript” lesson free?
Yes — the full text of “Decorators in TypeScript” is free to read here on the web, and the TypeScript Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “Decorators in TypeScript”?
Discover how decorators add metadata to your code and simplify development in frameworks like Angular. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start TypeScript Academy?
No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Decorators in TypeScript” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this TypeScript Academy lesson?
Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Type Inference and Contextual Typing
- Decorators in TypeScript
- TypeScript Modules and Namespaces