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

TypeScript · Decorator

ParameterDecorator

A parameter decorator is a runtime callback placed before a parameter in a class constructor or method. It receives the owning target, member name, and parameter index, and is primarily used to observe or record parameter metadata.

How it works

A parameter decorator is placed immediately before a parameter in a class constructor or method. At runtime, TypeScript invokes it with the relevant constructor or prototype, the member name, and the parameter’s zero-based position; its return value is discarded, so it observes or records information rather than directly replacing the parameter.

Syntax

function decorator(target: Function | Object, propertyKey: string | symbol, parameterIndex: number): void

Parameters

NameTypeDescription
targetFunction | ObjectClass constructor for a static member, or class prototype for an instance member.
propertyKeystring | symbolName of the decorated class member.
parameterIndexnumberZero-based ordinal position of the parameter in the function parameter list.

Returns

The decorator return value is ignored.

Key points

  • Place the decorator directly before the method or constructor parameter it annotates.
  • It applies to parameters belonging to class constructors or class methods.
  • The target identifies the owning constructor for static members or prototype for instance members.
  • The callback receives the decorated method or constructor member name.
  • The index identifies which parameter was decorated, using its position in the declaration.
  • The decorator expression resolves to a function that runs during class definition.

Common mistakes

  • It cannot transform the parameter or alter the method directly; use it for observation or metadata-style recording.
  • Do not place parameter decorators in .d.ts files, overload signatures, or declare-based ambient declarations.
  • Returning a replacement function or other value has no effect.

Example: Observing a method parameter

function required(target: object, propertyKey: string | symbol, parameterIndex: number) {
  console.log(`${String(propertyKey)} parameter ${parameterIndex} is required`);
}

class BugReport {
  print(@required text: string) {
    console.log(text);
  }
}

new BugReport().print("Fixed");

The decorator receives the class prototype, member name, and zero-based parameter position when the class is defined.

When to use it

Record required parameters

A framework needs to mark selected method arguments for validation before invoking the method.

Capture injection positions

A dependency-injection system needs to associate constructor or method positions with injection metadata.

Build parameter metadata

A tooling or runtime layer needs to inspect which argument positions carry annotations.

Interview questions

QWhat is a parameter decorator and what arguments does it receive?

A parameter decorator is written immediately before a parameter in a class constructor or method. Its runtime callback receives the constructor or prototype, the member name, and the parameter’s ordinal index.

Follow-up: What can it change? It cannot directly replace the parameter or method: the decorator is observational, and its return value is ignored.

QHow does the target differ for static and instance members?

For a static member, the first argument is the class constructor; for an instance member, it is the class prototype. The second argument identifies the member, and the third is the parameter’s position in the parameter list.

Follow-up: Why does the first argument differ? Static members belong to the constructor, whereas instance members belong to the prototype.

QAre parameter decorators supported by TypeScript’s standard decorators?

The documented legacy parameter-decorator form uses the experimental decorators model and is distinct from the newer standard decorators proposal. TypeScript 5.0’s standard decorators do not support parameter decoration.

Follow-up: What compatibility issue follows? Code using parameter decorators must use the legacy experimental-decorator support rather than the standard decorator implementation.

  • Decorators
  • Method Decorators
  • Property Decorators
  • experimentalDecorators
  • emitDecoratorMetadata

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