I encountered an issue while working with a third-party library where I needed to register my own control. The problem arose when I tried to add another dependency to the control and struggled with passing a closure as a parameter to fulfill the required constructor type.
The method for registration has the following signature:
class Registrator {
static Add(controlName: string, component: new (...params: any[]) => Control): void;
}
I used the following code successfully before refactoring:
//Old code - working
Registrator.Add("CountdownTimer", Controls.CountdownTimer);
//Trying to add Dependency - can't compile
const countdownTimerFormater = new Objects.Time.TimeFormater();
Registrator.Add("CountdownTimer", (...params: any[]) => return new Controls.CountdownTimer(<HTMLElement>(params[0]), countdownTimerFormater));
The compiler error states: Argument of type '(...params: any[])=>CountdownTimer' is not assignable to parameter of type 'new (params: any[])=>Control'. Type '(...params: any[])=> CountdownTimer' provides no match for signature 'new (...params:any[]): Control'.
In my case, the params are dependent on the context of the control and will always have a length of 1 to pass an HTMLElement
as the parent for the control. As everything in JavaScript functions, I believe there should be a way to pass a closure that meets the parameter requirements or somehow include my dependency within the params from my code.