Decorators are an experimental feature that may change in future releases.
A Decorator is a special kind of declaration that can be attached to class declaration, method, accessor, property, or parameter. Decorators use the form @expression, where expression must evaluate to a function that will be called at runtime with information about the decorated declaration.
Decorator Evaluation
There is a well defined order to how decorators applied to various declarations inside of a class are applied:
Parameter Decorators, followed by Method, Accessor, or Property Decorators are applied for each instance member.
Parameter Decorators, followed by Method, Accessor, or Property Decorators are applied for each static member.
Parameter Decorators, are applied for the constructor.
A Class Decorator is declared just before a class declaration. The class decorator is applied to the constructor of the class and can be used to observe, modify, or replace a class definition. A class decorator cannot be used in a declaration file, or in any other ambient context (such as on a declare class).
A Method Decorator is declared just before a method declaration. The decorator is applied to the Property Descriptor for the method, and can be used to observe, modify, or replace a method definition. A method decorator cannot be used in a declaration file, on an overload, or in any other ambient context (such as in a declare class).
The expression for the method decorator will be called as a function at runtime, with the following three arguments:
Either the constructor function of the class for a static member, or the prototype of the class for an instance member.
The name of the member.
The Property Descriptor for the member.
If the method decorator returns a value, it will be used as the Property Descriptor for the method.
function Response( extra?: Record<string, any> ): MethodDecorator {
return ( target: any, key: string | symbol, descriptor: TypedPropertyDescriptor<any> ) => {
const fn = descriptor.value;
console.log( 'Order: 1' );
descriptor.value = function() {
const data = fn.call( this );
return {
status: 0,
message : 'OK',
...extra,
data
}
};
return descriptor;
}
}
class HomeController {
@Response( { time : +new Date } )
indexAction() {
return {
name : 'Achilles'
}
}
}
console.log( 'Order: 2 );
const response = new HomeController().indexAction();
console.log( response );
An Accessor Decorator is declared just before an accessor declaration The accessor decorator is applied to the Property Descriptor for the accessor and can be used to observe, modify, or replace an accessor's definitions. An accessor decorator cannot be used in a declaration file, or in any other ambient context.
⚠️
Typescript disallows decorating both the get and set accessor for a single member. Instead, all decorators for the member must be applied to the first accessor specified in document order. This is because decorators apply to a Property Descriptor, which combines both the get and set accessor, not each declaration separately.
The expression for the accessor decorator will be called as a function at runtime, with the following three argument:
Either the constructor function of the class for a static member, or the prototype of the class for an instance member.
A Property Decorator is declared just before a property declaration. A property decorator cannot be used in a declaration file, or in any other ambient context.
The expression for the property decorator will be called as a function at runtime, with the following two arguments:
Either the constructor function of the class for a static member, or the property of the class for an instance member.
The name of the member.
⚠️
A Property Descriptor is not provided as an argument to a property decorator due to how property decorators are initialized in TypeScript. This is because there is currently no mechanism to describe an instance property when defining members of a prototype, and no way to observe or modify the initializer for a property. The return value is ignored too. As such, a property decorator can only be used to observe that a property of a specific name has been declared for a class.
A Parameter Decorator is declared just before a parameter declaration. The parameter decorator is applied to the function for a class constructor or method declaration. A parameter decorator cannot be used in a declaration file, an overload, or in any other ambient context.
The expression for the parameter decorator will be called as a function at runtime, with the following three arguments:
Either the constructor function of the class for a static member, or the prototype of the class for an instance member.
The name of the member.
The ordinal index of the parameter in the function's parameter list.
The return value of the parameter decorator is ignored.
import 'reflect-metadata';
function Body(): ParameterDecorator {
return ( target, key: string | symbol, index: number ) => {
console.log( { target, key, index } );
}
}
class A {
index( @Body() data: Record<string, any>, @Body() name: string, @Body() ...args: any[] ) {
}
}