Instrumentation
Add tracing, timing, or audit output around method calls without editing each method body.
TypeScript · Decorator
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.
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.
function methodDecorator(target: object, propertyKey: string | symbol, descriptor: PropertyDescriptor): PropertyDescriptor | void| Name | Type | Description |
|---|---|---|
target | object | The class constructor for a static method, or the class prototype for an instance method. |
propertyKey | string | symbol | The name of the decorated method. |
descriptor | PropertyDescriptor | The method's property descriptor; a decorator can inspect or modify it. |
A PropertyDescriptor optionally returned to install a replacement method definition; the decorator may also modify the supplied descriptor in place.
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.
Add tracing, timing, or audit output around method calls without editing each method body.
Replace or wrap a method implementation while preserving the decorated class API.
Inspect descriptor information to enforce conventions or attach method-level behavior.
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.
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.
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.
DecoratorPropertyDescriptorexperimentalDecoratorsClass DecoratorsAccessor DecoratorsProperty DecoratorsParameter Decorators