0Pricing
TypeScript Academy · Lesson

Decorators in Frameworks: NestJS Example

See how NestJS uses decorators for DI and routing.

Decorators in Frameworks: NestJS Example is a free TypeScript Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Welcome

This lesson covers decorators in frameworks nestjs example in TypeScript.

Introduction

Decorators are a stage-3 JavaScript proposal and an experimental TypeScript feature for adding metadata and modifying classes and their members.

Core Concept

A decorator is a function applied to a class, method, property, or parameter using the @ syntax.
@sealed
class BugReport {
  type = 'report';
  title: string;
  constructor(t: string) { this.title = t; }
}

Class Decorator

Class decorators receive the constructor function and can return a modified version.
function sealed(constructor: Function) {
  Object.seal(constructor);
  Object.seal(constructor.prototype);
}

Method Decorator

Method decorators receive the target, property name, and property descriptor.
function log(target: any, key: string, desc: PropertyDescriptor) {
  const orig = desc.value;
  desc.value = function(...args: any[]) {
    console.log('Calling', key);
    return orig.apply(this, args);
  };
}

Property Decorator

Property decorators receive the target and property key. They cannot intercept reads/writes directly.
function readonly(target: any, key: string) {
  Object.defineProperty(target, key, { writable: false });
}

Parameter Decorator

Parameter decorators receive target, method name, and parameter index.
function required(target: any, key: string, index: number) {
  const meta = Reflect.getMetadata('required', target, key) || [];
  meta.push(index);
  Reflect.defineMetadata('required', meta, target, key);
}

Decorator Factories

A decorator factory is a function that returns a decorator. It takes configuration parameters.
function role(name: string) {
  return function(target: any, key: string) {
    Reflect.defineMetadata('role', name, target, key);
  };
}

class UserController {
  @role('admin')
  deleteUser() {}
}

NestJS Decorators

NestJS uses decorators extensively for controllers, dependency injection, and routing.
@Controller('users')
class UsersController {
  constructor(private usersService: UsersService) {}

  @Get()
  findAll(): User[] {
    return this.usersService.findAll();
  }
}

reflect-metadata

Decorators often work with the reflect-metadata polyfill to store and retrieve metadata at runtime.
import 'reflect-metadata';
// Enable in tsconfig: experimentalDecorators: true, emitDecoratorMetadata: true

Decorator Order

Multiple decorators on the same declaration are applied bottom-up (the last written is evaluated first).
@first
@second
class Example {}
// Applied: second first, then first

Quick Check

What TypeScript configuration is required to use decorators?

Recap

Decorators use @ syntax to modify classes, methods, properties, and parameters. They are enabled with experimentalDecorators in tsconfig and widely used by frameworks like NestJS and Angular.

Frequently asked questions

Is the “Decorators in Frameworks: NestJS Example” lesson free?

Yes — the full text of “Decorators in Frameworks: NestJS Example” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Decorators in Frameworks: NestJS Example”?

See how NestJS uses decorators for DI and routing. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Decorators in Frameworks: NestJS Example” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Class Decorators: Wrapping Behavior
  2. Method and Property Decorators
  3. Parameter Decorators and Metadata
  4. Decorators in Frameworks: NestJS Example
← Back to TypeScript Academy