Decorators add metadata and modify behavior declaratively. Class decorators transform class definitions. Method decorators wrap or modify methods. Property decorators manage property access and metadata.
Decorator Types
Class decorators receive constructor functions. Method decorators receive target, property key, and descriptor. Property decorators receive target and property key. Parameter decorators add metadata to parameters.
- Class decorators modify or wrap class constructors
- Method decorators intercept method calls
- Property decorators manage property access
- Use reflect-metadata for storing metadata
- Combine decorators for layered functionality
Practical Applications
Logging decorators trace method calls. Validation decorators enforce constraints. Caching decorators memoize results. Dependency injection uses decorators extensively.