04 / 07

What are the different types of decorators and their execution order in TypeScript?

TypeScript supports class, method, accessor, property, and parameter decorators. Execution order is bottom-to-top (outer to inner): parameter decorators run first, then method decorators, then class decorators. When multiple decorators stack on a method, they compose right-to-left like function composition.

Decorator execution order demonstration
Execution order summary:
  1. 1

    Parameter decorators run before method decorators on the same member.

  2. 2

    Multiple decorators on the same target: factories evaluated top-to-bottom, then applied bottom-to-top.

  3. 3

    Member decorators (methods, properties) run before class decorators.

  4. 4

    Class decorator is always the last to execute — it sees the fully-decorated class.

Difficulty: 5/10
Topics: class decorators, method/property decorators, execution order

Scenario Questions

0-2 years experience
  1. 1

    In a NestJS controller, you need to log the execution time of a route handler. Which decorator would you create and how would you apply it?

  2. 2

    If you place a @UseGuards() decorator above a @Controller() class and also above a specific method, which one runs first and why?

2-5 years experience
  1. 1

    You added a custom @Roles() method decorator to a route, but the guard isn’t receiving the roles metadata. Walk me through how decorator execution order could cause this and how you’d fix it.

  2. 2

    During a refactor, you moved a @Transactional() method decorator to a base class. The transaction no longer starts. Explain what might have changed in the decorator order and how to debug it.

5-8 years experience
  1. 1

    Our microservice uses many custom class and method decorators for validation, logging, and tracing. How would you structure their order to avoid performance penalties and ensure correct metadata propagation?

  2. 2

    We observed that applying multiple property decorators on a DTO sometimes results in missing validation rules at runtime. Discuss the execution order of property decorators and how you’d design a solution to guarantee all validators run.

8+ years experience
  1. 1

    We are planning to migrate a large legacy NestJS codebase to a new shared decorator library that enforces company‑wide policies. What architectural considerations and ordering strategies would you adopt to minimize breaking changes across teams?

  2. 2

    Imagine you need to introduce a global request‑id decorator that must run before any logging or authentication decorators across all services. How would you enforce this order at the framework level and what trade‑offs does it entail?

Follow-up Questions

  • What would happen if you swapped the order of two method decorators?
  • Can you illustrate with a short code snippet how metadata is attached?
  • How does NestJS use Reflect.metadata under the hood for these decorators?