How can I pass an array of functions that return an object of some type. In other words, an array of constructors?
I want to do something like this:
constructor(name: string, systems: Array<Function>){
this.system = new systems[0]();
}
but I received an error message Cannot use 'new' with an expression whose type lacks a call or construct signature., and as I understand it, I have to somehow tell the compiler what type of objects is returned to the constructor, but I donβt know how to do it.
Assuming RJM's answer , here is a slightly extended example of what I mean:
interface System{
name: string;
}
interface Component{
blahblha: string;
}
class Doh implements Component{
blahblha: string;
}
class Bar implements System{
name: string = 'bar';
}
class Blah implements System{
name: string = 'foo';
}
class Foo {
systems: Array<System>;
constructor(bars: Array<()=>System>) {
for (let i in bars){
this.systems.push(new bars[i]());
}
}
}
var foo = new Foo([Blah]);
I want to make sure that Foo.systemsonly instances of objects that implement the system interface will be populated. So I have to make a mistake doing something like new Foo([Doh]);, but I get an error even when transferringBlah
source
share