Course contentsShow
← JavaScript
Lesson 2102 of 2,72343. DecoratorsFree lesson

Legacy Class and Method Decorators

Writing decorators for classes and methods using TypeScript's original syntax and parameter order.

Legacy Class and Method Decorators

You'll learn how to write decorators using TypeScript's original experimental syntax, which differs in signature and parameter order from the Stage 3 standard.

The Original Syntax

Before the Stage 3 decorators proposal stabilized, TypeScript introduced its own decorator syntax (enabled via experimentalDecorators). While the application syntax looks identical (@decorator), the decorator function signatures are completely different.

Legacy Class Decorator

A legacy class decorator receives just the constructor function as its single parameter:

function sealed(constructor: Function) {
  Object.seal(constructor);
  Object.seal(constructor.prototype);
}

@sealed
class MyClass {}

Think of it as receiving the raw blueprint itself—no context object, just the constructor.

Legacy Method Decorator

Method decorators receive three parameters in this specific order:

function readonly(
  target: any,           // Prototype (or constructor for static)
  propertyKey: string,   // Method name
  descriptor: PropertyDescriptor
) {
  descriptor.writable = false;
}

class Example {
  @readonly
  method() {
    return "cannot change this";
  }
}

The parameters mirror JavaScript's Object.defineProperty order: target, key, descriptor. No context object exists here—you manipulate the descriptor directly and either return a new one or modify it in place.

Key Differences

  • No context object: Stage 3 provides rich metadata; legacy gives you raw JavaScript constructs
  • Parameter order: Legacy mirrors Object.defineProperty; Stage 3 uses a unified context approach
  • Return values: Handled differently between the two systems

Key Takeaway: Legacy decorators use TypeScript's original signatures—class decorators receive only the constructor, while method decorators receive target, key, and descriptor in that order, mirroring traditional JavaScript property definition APIs.