advanced

Decorators and metadata

Recognize decorators as framework-facing metadata hooks that can improve declarative APIs but also hide control flow and complicate typing.

TypeScript decorators attach metadata and cross-cutting behavior to classes, methods, and parameters—common in NestJS (`@Controller`, `@Injectable`) and legacy Angular. They enable declarative routing, validation, and DI registration but hide imperative wiring from readers.

Experimental decorator stages and reflect-metadata affect how frameworks discover types at runtime.

On interviews: trace how a Nest route decorator connects to a controller method and name downsides (magic, harder testing, typing edge cases).

Common pitfalls: business logic inside decorators, decorator order surprises, and relying on emitDecoratorMetadata without understanding limits.

The trade-off is concise framework DSL versus opaque control flow for newcomers.

Checklist:

  • Know your TS decorator stage and config.
  • Keep decorators thin—wire, don't implement rules.
  • Test behavior, not decorator presence alone.