How to add parameters to a class decorator in TypeScript?
12,828
If you want your decorator to receive parameters then your decorator function needs to return the actual decorator function:
function PluginDecorator(name: string) {
return (ctor: Function) => {
console.log("Plugin found: " + name);
}
}
@PluginDecorator("My first Plugin")
class myFirstPlugin {}
I changed the name to PluginDecorator
because Plugin
already exists and the compiler complains about that name.
![mvermand](https://i.stack.imgur.com/bxEnK.jpg?s=256&g=1)
Author by
mvermand
Software Architect at AXI n.v. and owner of http://www.codessentials.com and http://www.resolvethis.com
Updated on June 03, 2022Comments
-
mvermand about 2 years
I want to create a decorator function for a class that can take a parameter.
Example
@Plugin("My first Plugin") class myFirstPlugin { ... }
I tried this, but it does not work:
function Plugin(constructor: Function, name:string){ console.log("Plugin found: " + name); }
I get an error in WebStorm saying:
TS2346: Supplied parameters do not match any signature of call target
How do I need to write this decorator function?