'How to add parameters to a class decorator in TypeScript?
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?
Solution 1:[1]
If you need to access both the parameters and the constructor:
type BaseClass = HTMLElement;
type Constructor = { new (...args: any[]): BaseClass };
export function Plugin(name: string) {
return function <T extends Constructor>(constructor: T) {
const cls = class extends constructor {
// custom implementation here
};
// perhaps something like this
customElements.define(name, cls, { extends: "div" });
return cls;
};
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Graeme Wicksted |
