TypeScript Academy · Lesson

Method and Property Decorators

Decorate individual methods and properties.

Lesson 2 of 413 steps

Method and Property Decorators is a free TypeScript Academy lesson on CoddyKit. This is lesson 2 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, and your progress syncs across the web and the CoddyKit app. The TypeScript Academy course includes 4 lessons in total.

Welcome

This lesson covers method and property decorators 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.
Free to start

Learn TypeScript with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
101
Lessons
352

Frequently Asked Questions

Is the “Method and Property Decorators” lesson free?

Yes — the full text of “Method and Property Decorators” is free to read here on the web. 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. The TypeScript Academy course includes 4 lessons in total.

What will I learn in “Method and Property Decorators”?

Decorate individual methods and properties. 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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “Method and Property Decorators” 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