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

Property and Parameter Decorators in Legacy Syntax

Creating property and parameter decorators with TypeScript's experimental decorator model.

Property and Parameter Decorators in Legacy Syntax

What You'll Learn

You'll learn how TypeScript's experimental decorator model handles property and parameter decorators, which differ significantly from the Stage 3 proposal.

Property Decorators in Legacy Syntax

In the legacy experimental decorator model, property decorators receive two arguments:

  1. The target (constructor function for static members, or prototype for instance members)
  2. The property name (string or symbol)

Unlike method decorators, property decorators do not receive a property descriptor. They also cannot return a value that affects the property definition. This makes them primarily useful for metadata collection rather than transformation.

function logProperty(target: any, propertyKey: string) {
  console.log(`Property "${propertyKey}" declared on`, target);
}

class User {
  @logProperty
  username: string = "guest";
}

Think of property decorators as observers that watch properties being defined but can't modify them directly.

Parameter Decorators in Legacy Syntax

Parameter decorators are even more limited. They receive three arguments:

  1. The target (constructor or prototype)
  2. The method name containing the parameter
  3. The parameter's index position

They exist purely for metadata reflection—they cannot change parameter behavior or return anything meaningful.

function logParameter(target: any, methodName: string, paramIndex: number) {
  console.log(`Parameter at index ${paramIndex} in method "${methodName}"`);
}

class Calculator {
  add(@logParameter x: number, @logParameter y: number) {
    return x + y;
  }
}

These decorators are typically used with reflect-metadata to store type information for dependency injection frameworks.

Key Limitations

Both property and parameter decorators in legacy syntax are metadata-only tools—they observe but rarely transform, making them less powerful than their Stage 3 counterparts.

Key Takeaway: Legacy property and parameter decorators are primarily metadata collectors with limited transformation capabilities, receiving target references and names/indices but offering no way to modify the decorated elements directly.