Decorators — TypeScript

A decorator is a function attached with @name to a class or a method, to add behaviour without changing what's inside. The standard version (TypeScript 5.0+…

You want every method in the Cashier class to write its arguments and result to the console. The class Cashier is written like the JavaScript classes you already know. What TypeScript adds to classes is covered in the next lesson. If you put console.log at the start and end of every method, the methods get messy, and it is easy to forget one.

A decorator solves this from outside the method. A decorator is a function you attach to a class or a method with the @ sign. It adds behaviour without changing what's inside the method. It's like gift wrapping: the present inside (the method) stays the same, but the wrapping (the decorator) adds something, such as a card that is read every time the present is opened. The card is a line in the console, and opening the present is calling the method. In this lesson, the @logged decorator wraps the total method: it still calculates price times quantity, but now every call is written to the console.

Method decorators: take the method, return its replacement

Since TypeScript 5.0, decorators follow the JavaScript standard proposal from TC39, the committee that decides on new JavaScript features. This version, known as Stage 3 decorators, needs no flag (a special option you must turn on). The proposal is not final: since May 2026 it is back at stage 2.7.

Node cannot run @ yet, so node file.ts fails with SyntaxError. That's why tsc must turn decorators into plain JavaScript. The target option in tsconfig (the TypeScript settings file) sets which JavaScript version tsc writes. Use a version such as es2022, not the esnext (the newest JavaScript) that tsc --init writes: with esnext, the @ is left as it is in the compiled output.

A method decorator is an ordinary function with two parameters:

function logged(method: (...args: number[]) => number, context: ClassMethodDecoratorContext) {
  const name = String(context.name)
  return function (this: unknown, ...args: number[]): number {
    console.log(`-> ${name}(${args.join(", ")})`)
    const result = method.apply(this, args)
    console.log(`<- ${name} = ${result}`)
    return result
  }
}

class Cashier {
  @logged
  total(price: number, quantity: number): number {
    return price * quantity
  }
}

const cashier = new Cashier()
cashier.total(15, 2)

This is what the console prints:

-> total(15, 2)
<- total = 30

When a decorator runs

Guess first: if cashier.total(...) is called 3 times, how many times does the logged function itself run? 1, 3 or 4?

The logged function runs once, when the class Cashier is created (when JavaScript runs class Cashier { ... }), before anyone calls total. What runs on every call is the replacement function that logged returned.

When one method has two decorators, they're written one above the other. The method's body is shortened to { ... }:

class Cashier {
  @first
  @second
  total(price: number, quantity: number): number { ... }
}

The decorator closest to the method is applied first: second wraps total, then first wraps the replacement function from second.

Class decorators

A decorator can be attached to a class too. A class decorator receives the class itself (the parameter value) and a context with kind: "class":

const registry: string[] = []

function registered(value: Function, context: ClassDecoratorContext) {
  registry.push(String(context.name))
}

@registered
class Cashier { ... }

console.log(registry) // ["Cashier"]

As soon as the class Cashier is created, its name goes into registry. Frameworks (ready-made structures for applications) use this pattern to register classes, such as @Component in Angular or @Controller in NestJS.

Two versions: standard and experimentalDecorators

Before TypeScript 5.0, there was only an older version of decorators, turned on with "experimentalDecorators": true in tsconfig. That older version differs from the standard. The frameworks that used decorators early, such as Angular, NestJS and TypeORM (a library for databases), were built on that older version. That's why NestJS and TypeORM projects still require the experimentalDecorators flag.

Standard (TypeScript 5.0+) experimentalDecorators: true
A method decorator receives (method, context) (target, propertyKey, descriptor)
It returns a replacement function the descriptor (with descriptor.value replaced)
Decorators on constructor parameters none yes, for example @Inject()
emitDecoratorMetadata not supported supported

emitDecoratorMetadata is a tsconfig option that keeps type information in the compiled JavaScript, for a framework to read.

This is the same logged, written in the older version. The method's name is in propertyKey, the original method is in descriptor.value, and the replacement is written to the same place:

// tsconfig: "experimentalDecorators": true
function logged(target: object, propertyKey: string, descriptor: PropertyDescriptor) {
  const original = descriptor.value
  descriptor.value = function (this: unknown, ...args: unknown[]) {
    console.log(`-> ${propertyKey}(${args.join(", ")})`)
    const result = original.apply(this, args)
    console.log(`<- ${propertyKey} = ${result}`)
    return result
  }
  return descriptor
}

The console output is exactly the same. The only difference is what the decorator receives and returns.

⚠️ Two versions mixed up, and a lost this

TL;DR: A decorator is a function attached with @name to a class or a method, to add behaviour without changing what's inside. The standard version (TypeScript 5.0+, no flag or special option) gives a method decorator two things: the original method and a context, an object that describes it (kind, name, ...). Whatever the decorator returns replaces that method. The decorator function runs once, when JavaScript runs the class's code, and its replacement runs on every call. The older version needs "experimentalDecorators": true in tsconfig (the TypeScript settings file) and receives (target, propertyKey, descriptor). NestJS and TypeORM (a framework and a library for servers) still require that flag, because both were built on the older format, which differs from the standard. Don't mix the two versions.

⚡ Attach it with @

Write:

class Cashier {
  @logged
  total(price: number, quantity: number): number {
    return price * quantity
  }
}

Meaning: logged is a decorator: a function attached with @ to add behaviour to a method without changing what's inside it. @logged hands the total method to logged, along with a context (an object that holds the method's name). Whatever logged returns replaces total: it still calculates price times quantity, but every call is now written to the console. Inside logged, the replacement is written with function, not an arrow function, so this stays the object that called the method.

⚡ A decorator runs once

Guess first: cashier is an object made with new Cashier(). If cashier.total(...) is called 3 times, how many times does the logged function itself run?

Once, when the Cashier class is created (when JavaScript runs class Cashier { ... }), before anyone calls total. What runs 3 times is the replacement function that logged returned.

Meaning: Work for each call goes in the replacement function, not in the body of the decorator function.

⚡ Two versions you mustn't mix

Write:

"experimentalDecorators": true

Meaning: This line is a flag (a special option) in tsconfig, the TypeScript settings file. Since TypeScript 5.0, standard decorators need no flag, and a standard method decorator receives (method, context). This flag turns on the old version, which receives (target, propertyKey, descriptor). Angular, NestJS and TypeORM (popular frameworks and a library) were built on that old format, which is why they still ask for this flag. Check your library's docs for which version it uses.

What you will learn