Skip to content

Decorator

In one line

A decorator is a TypeScript feature (experimental) that modifies or enhances classes, methods, properties, or parameters using a @decorator syntax.

In simple words

Decorators are functions that modify classes and their members. You apply them with the @ syntax: @log above a method. They are an experimental feature that requires enabling experimentalDecorators in tsconfig.

Decorators are commonly used for logging, validation, dependency injection, and ORM mapping. Frameworks like NestJS and TypeORM rely heavily on decorators.

There are several types: class decorators, method decorators, property decorators, and parameter decorators. Each receives different arguments describing the target being decorated. The decorator standard is still evolving (TC39 proposal).

Example

typescript
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);
  };
}

class Calc {
  @log add(a: number, b: number) { return a + b; }
}

The @log decorator wraps the add method to log each call.

Related terms

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary