After reviewing the typescript decorators documentation, it is noted that the example for replacing a constructor does not involve passing any arguments to the decorator function. How can I achieve this differently?
This is the relevant snippet from the documentation:
function classDecorator<T extends {new(...args:any[]):{}}>(constructor:T) {
return class extends constructor {
newProperty = "new property";
hello = "override";
}
}
@classDecorator
class Greeter {
property = "property";
hello: string;
constructor(m: string) {
this.hello = m;
}
}
Now, I wish to pass arguments by utilizing the decorator as shown below:
@classDecorator({
// some object properties fitting DecoratorData type
})
My attempt at adding a second data parameter looked like this:
function classDecorator<T extends {new(...args:any[]):{}}>(constructor: T, data: DecoratorData)
However, this resulted in a "2 arguments required, got 1" tslint error. Even inserting a null
placeholder did not solve the issue. I also experimented with using the constructor
parameter as a second optional parameter, but this led to a signature mismatch error.