Docs
/
TypeScript
Chapter 18
18 — Decorators
Core Concepts
- Decorators — functions that modify classes, methods, properties, or parameters
- Class decorators — applied to the class constructor
- Method decorators — applied to methods
- Property decorators — applied to properties
- Parameter decorators — applied to method parameters
- Decorator factories — functions that return decorators (for configuration)
- TC39 Stage 3 decorators — the new standard (TS 5.0+)
- Legacy decorators —
experimentalDecorators: true(older libraries like TypeORM, NestJS)
Decorator Types
| Decorator | Applied To | Receives |
|---|---|---|
| Class | @Dec class Foo | Constructor function |
| Method | @Dec method() | target, propertyKey, descriptor |
| Property | @Dec prop | target, propertyKey |
| Parameter | method(@Dec param) | target, propertyKey, parameterIndex |
Note
Enable with "experimentalDecorators": true in tsconfig for legacy decorators.
TS 5.0+ supports TC39 Stage 3 decorators natively (no flag needed).