I have this simple class with a property that has a property decorator applied to it:
class MyClass {
@collectionMember
public myProperty: number[];
// ...
}
And the decorator function:
function collectionMember(target: Object, propertyKey: string | symbol): void {
// ...
}
How can I pass additional arguments to the decorator function? I tried doing the following with no avail:
class MyClass {
@collectionMember("MyProp")
public myProperty: number[];
// ...
}
Obviously, this yields the error
Supplied parameters do not match any signature of call target.
Using Decorator Syntax In TypeScript, you can create decorators using the special syntax @expression , where expression is a function that will be called automatically during runtime with details about the target of the decorator. The target of a decorator depends on where you add them.
NOTE: Decorators are an experimental feature and are still in stage 2 in JavaScript, also, experimental in TypeScript, so it might change in the future.
It's possible to use as many decorators on the same piece of code as you desire, and they'll be applied in the order that you declare them. This defines a class and applies three decorators — two to the class itself, and one to a property of the class: @log could log all access to the class.
The TypeScript website describes it as: “A Decorator is a special kind of declaration that can be attached to a class declaration, method, accessor, property, or parameter.” I would describe it as “a special declaration to add extra features on an existing class declaration, method, accessor, property, or parameter.”
It can be done by using a decorator factory.
The factory, is just a function that receives any parameters you want and returns a function with a decorator signature:
// any parameters, even optional ones!
function collectionMember(a: string, b?: number) {
// the original decorator
function actualDecorator(target: Object, property: string | symbol): void {
// do something with the stuff
console.log(a);
console.log(target);
}
// return the decorator
return actualDecorator;
}
And then you can use it as you described.
class MyClass {
@collectionMember('MyProp') // 2nd parameter is not needed for this array
public myProperty: number[] = [1, 2, 3, 4, 5];
// ...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With