TypeScript decorators (now Stage 3 standard) attach metadata and transform classes, methods, properties, and parameters at declaration time. Class decorators receive the class constructor. Method decorators receive the class prototype + property descriptor. Decorators compose from bottom to top (inner to outer). NestJS uses decorators extensively: @Controller, @Get, @Injectable, @Body. Reflect.metadata stores and retrieves decorator-attached metadata.
How NestJS uses decorators to define a controller.
Decorator evaluation happens at class definition time (module load), not at call time. Reflect.metadata stores configuration as runtime metadata on the prototype, read later by frameworks.
With @A @B methodFn(), B is applied first (closer to the method), then A wraps B. This matters for decorator chains: put authentication before authorization in reading order (they apply inside-out).
Most real decorators are factories: @Role('admin') is a function call that returns the decorator. This allows passing configuration like timing, caching TTL, or permission levels.
Sign in to share your feedback and join the discussion.