Reflection Metadata & Typed Decorators
Reflection Metadata & Typed Decorators
Metadata is how NestJS turns decorators into runtime decisions. A decorator can attach roles, feature flags, cache keys, audit labels, or public-route markers; a guard or interceptor can then read that metadata and apply policy consistently.
Core idea
This feature is about controlling how the application is organized and how it behaves at runtime. These are the points a developer should understand before using it in a real project:
- SetMetadata() works, but Reflector.createDecorator() gives stronger typing and removes string-key mistakes.
- getAllAndOverride() is ideal when method metadata should replace controller metadata.
- getAllAndMerge() is useful when class-level and method-level metadata should combine.
- Metadata belongs to orchestration decisions; do not hide business rules that belong in services.
- Composed decorators with applyDecorators() keep controllers readable when a route needs guards, roles, OpenAPI metadata, and interceptors together.
Practical example
The following example shows the idea in a practical NestJS project. The goal is not to memorize the snippet, but to understand where it belongs in the architecture:
Production checklist
- Use typed decorator factories for repeated metadata keys.
- Choose override vs merge semantics deliberately.
- Do not create decorators that perform database work directly.
- Keep composed decorators small enough that their behavior is still obvious at the route.
Summary
This lesson covers an advanced NestJS area that matters when building enterprise applications. Focus on clear boundaries, testable behavior, and choosing the right tool for the context instead of using every feature everywhere.