01 / 07

What is a decorator in TypeScript and how does NestJS use decorators?

A decorator is a function that wraps a class, method, property, or parameter and can attach metadata or modify behavior. NestJS uses decorators extensively — @Module(), @Injectable(), @Controller(), @Get(), @Body() are all decorators — enabled by experimentalDecorators: true in tsconfig.

Decorators are a stage-3 JavaScript proposal and an experimental TypeScript feature. They allow you to annotate and modify classes and their members at design time. NestJS is built entirely around decorators as its primary API.

Decorator types used in NestJS
How NestJS uses decorators:
  1. 1

    @Module() — defines module metadata (imports, providers, exports, controllers).

  2. 2

    @Injectable() — marks a class as a provider eligible for DI.

  3. 3

    @Controller() — marks a class as a request handler with a route prefix.

  4. 4

    HTTP decorators (@Get, @Post, etc.) — map methods to HTTP verbs and routes.

  5. 5

    Parameter decorators (@Body, @Param, @Query) — extract data from requests.