Sometimes, we might need to customize the way our decorator operates upon instancing it. No worries! We can design our decorators with custom signatures and then have them returning a function with the same signature we defined when designing class decorators with no parameters. As a rule of thumb, decorators taking parameters just require a function whose signature matches the parameters we want to configure. Such a function must return another function, whose signature matches that of the decorator we want to define.
The following piece of code illustrates the same functionality as the previous example, but it allows developers to customize the greeting message:
function Banana(message: string) {
return function(target: Function) {
target.prototype.banana = function(): void {
console.log(message);
}
}
}
@Greeter('Bananas are yellow!')
class FruitBasket {
constructor() {
// Implementation goes here...
}
}
var basket = new FruitBasket();
basket.banana(); // console will output 'Bananas are yellow'