I have an interface:
interface I {
new(name: string, ...args: any[]);
}
And classes with constructors like this:
constructor(name: string) { }
constructor(name: string, a: number) { }
constructor(name: string, a: string, b: string, c: string, d: string) { }
constructor(name: string, x: { b: boolean }) { }
It always tells me the implementation does not fit the interface.
I thought it would be sufficient to use , ...args: any[]
but it's not.
This is a syntax error: , ...args?: any[]
.
Even when I have all rest arguments optional, like explained in this answer it yield the same error:
constructor(name: string, a?, b?, c?) { }
In one of those answers, someone advices to just not use a constructor prototype in the interface (yes, I also tried overloading without success). But it would be nice to have a constructor interface with at least this name: string
and the rest to be optional.
How can I have constructors with 1 mandatory and 0-n optional arguments?