Record required parameters
A framework needs to mark selected method arguments for validation before invoking the method.
TypeScript · Decorator
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.
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.
function decorator(target: Function | Object, propertyKey: string | symbol, parameterIndex: number): void| Name | Type | Description |
|---|---|---|
target | Function | Object | Class constructor for a static member, or class prototype for an instance member. |
propertyKey | string | symbol | Name of the decorated class member. |
parameterIndex | number | Zero-based ordinal position of the parameter in the function parameter list. |
The decorator return value is ignored.
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.
A framework needs to mark selected method arguments for validation before invoking the method.
A dependency-injection system needs to associate constructor or method positions with injection metadata.
A tooling or runtime layer needs to inspect which argument positions carry annotations.
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.
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.
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.
DecoratorsMethod DecoratorsProperty DecoratorsexperimentalDecoratorsemitDecoratorMetadata