Sergei Basharov Sergei Basharov - 4 months ago 17x
Javascript Question

How to type an array with classes in TypeScript?

I have an app that initializes by running its method

like this:

app.init([TopBar, StatusBar, MainArea]);

are classes, not instances of classes. Each of these classes implements the same interface

I want to instantiate objects from the passed classes in the
method, like this:

init(params: IComponent[]): void { (component) {
let comp = new component();
this.components[] = comp;
}, this);

The issue is that as these are not instance, TypeScript doesn't know their types and throws an error:

error TS2345: Argument of type '(typeof TopBar | typeof StatusBar |
typeof MainArea)[]' is not assignable to parameter of type

How do I fix the code so that I could pass an array of classes that implement some interface to a method?


There is a working typescript playground (run it to get alert with result)

what we need is to create a custom type InterfaceComponent. That will be expected as an array of the init() method

interface IComponent { }
class TopBar    implements IComponent { }
class StatusBar implements IComponent { }
class MainArea  implements IComponent { }

// this is a type we want to be passed into INIT as an array
type InterfaceComponent = (typeof TopBar | typeof StatusBar | typeof MainArea);

class MyClass {

  components: {[key:string] : IComponent } = {};

  init(params: (InterfaceComponent)[]): void { => {
        let comp = new component();
        this.components[comp.constructor["name"]] = comp;
    }, this);

let x = new MyClass();
x.init([TopBar, StatusBar, MainArea])


Check it here