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

TypeScript · Decorator

MethodDecorator

A legacy method decorator is placed before a method and receives its target, name, and property descriptor at runtime. It can observe, modify, or replace the method definition.

How it works

A legacy method decorator is placed immediately before a method and receives the class prototype for an instance method, the member name, and its property descriptor. It can inspect the method, alter the descriptor, or replace the method definition by returning a descriptor.

Syntax

function methodDecorator(target: object, propertyKey: string | symbol, descriptor: PropertyDescriptor): PropertyDescriptor | void

Parameters

NameTypeDescription
targetobjectThe class constructor for a static method, or the class prototype for an instance method.
propertyKeystring | symbolThe name of the decorated method.
descriptorPropertyDescriptorThe method's property descriptor; a decorator can inspect or modify it.

Returns

A PropertyDescriptor optionally returned to install a replacement method definition; the decorator may also modify the supplied descriptor in place.

Key points

  • Place the decorator directly before the method declaration.
  • The legacy API centers on the method's PropertyDescriptor, which provides access to its definition.
  • Use a method decorator for observation, descriptor changes, or replacement of the method.
  • The first runtime argument identifies whether the decorated method belongs to the class itself or its prototype.
  • The second runtime argument is the decorated member's name.
  • The third runtime argument is the member's property descriptor.

Common mistakes

  • Do not apply a method decorator to declaration files, overload signatures, or ambient declarations such as declare classes.
  • This legacy decorator form requires the experimentalDecorators compiler option.

Example: Wrapping an instance method

function logCall(
  target: object,
  name: string,
  descriptor: PropertyDescriptor
): PropertyDescriptor {
  const original = descriptor.value;
  descriptor.value = function (...args: unknown[]) {
    console.log(`${name} called`);
    return original.apply(this, args);
  };
  return descriptor;
}

class Greeter {
  @logCall
  greet(name: string) {
    return `Hello, ${name}`;
  }
}

console.log(new Greeter().greet("Ada"));

The decorator wraps the method implementation through its property descriptor, logs each call, and preserves the returned value.

When to use it

Instrumentation

Add tracing, timing, or audit output around method calls without editing each method body.

Behavior substitution

Replace or wrap a method implementation while preserving the decorated class API.

Cross-cutting method policies

Inspect descriptor information to enforce conventions or attach method-level behavior.

Interview questions

QWhat arguments does a legacy method decorator receive?

A method decorator is declared immediately before a method. At runtime, its function receives the class constructor for a static method or the prototype for an instance method, followed by the member name and property descriptor.

Follow-up: How can it change behavior? Modify the descriptor, commonly by replacing descriptor.value, or return a property descriptor that installs a replacement definition.

QWhat can a method decorator do?

It can observe, modify, or replace the method definition because it operates on the method's property descriptor. A wrapper can replace descriptor.value and delegate to the original implementation.

Follow-up: What must be preserved in a wrapper? Preserve the receiver and arguments when forwarding, and return the original result when the wrapper is intended to be transparent.

QWhat is the relevant decorator application order?

For instance members, decorators on parameters are applied before method, accessor, or property decorators for that member. Static-member decorators are processed afterward, followed by constructor parameter decorators and then the class decorator.

Follow-up: Why does ordering matter? Multiple decorators can observe or transform the same declaration, so their application order affects which descriptor or definition each decorator receives.

  • Decorator
  • PropertyDescriptor
  • experimentalDecorators
  • Class Decorators
  • Accessor Decorators
  • Property Decorators
  • Parameter Decorators

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