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 decoratorsexperimentalDecorators: true (older libraries like TypeORM, NestJS)

Decorator Types

DecoratorApplied ToReceives
Class@Dec class FooConstructor function
Method@Dec method()target, propertyKey, descriptor
Property@Dec proptarget, propertyKey
Parametermethod(@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).