Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

TypeScript · Decorator

PropertyDecorator

A legacy property decorator is a runtime callback attached immediately before a class property. It receives the class constructor or prototype and the property name, so it can observe the declaration but cannot directly modify its descriptor.

How it works

A legacy property decorator is placed immediately before a class property. At runtime, TypeScript calls it with the class constructor for a static property or the class prototype for an instance property, plus the property name. It can observe the declaration, but it cannot directly replace or redefine the property because no descriptor is provided.

Syntax

function propertyDecorator(target: Function | Object, propertyKey: string | symbol): void

Parameters

NameTypeDescription
targetFunction | ObjectThe class constructor for a static property, or the class prototype for an instance property.
propertyKeystring | symbolThe declared property name.

Returns

The decorator callback returns void; the documented legacy property-decorator protocol does not provide a property descriptor to replace.

Key points

  • Write the decorator directly before the property it decorates.
  • The decorator expression runs at runtime when the decorated class is evaluated.
  • The legacy callback receives two arguments: the target object and the property name.
  • Static properties use the class constructor as the target; instance properties use the prototype.
  • The second callback argument identifies the decorated property by name.
  • This legacy decorator is observational rather than a mechanism for replacing the property.

Common mistakes

  • Property decorators are unavailable in declaration files and other ambient contexts, including declare classes.
  • This decorator form observes a named property declaration; it cannot directly alter the property's descriptor or implementation.
  • The first argument differs for static and instance properties: it is the constructor for static members and the prototype for instance members.

Example: Observing a property declaration

function logProperty(target: object, propertyKey: string | symbol): void {
  console.log(String(propertyKey));
}

class User {
  @logProperty
  name = "Ada";
}

new User();

The decorator observes the declared property name when the class is evaluated.

When to use it

Registration metadata

Record property names for a registration or metadata system during class evaluation.

Declaration observation

Inspect whether specific instance or static properties are declared.

Framework integration

Build lightweight framework conventions around decorated class members.

  • Decorator
  • MethodDecorator
  • ParameterDecorator
  • experimentalDecorators
  • Class Decorators

Source: Documentation - Decorators - TypeScript. Checked 2026-10-15.