Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define an array of classes in Typescript (not instances)

Tags:

typescript

I am trying out some language constructs in Typescript. I am looking to make an array of classes, where the classes are later instantiated. The code I have seems to compile and works if I try it on the Typescript Playground but it does give an error in the typescript box: Argument of type 'typeof Greeter' is not assignable to parameter of type 'BaseGreeter'.

I guess this is because the array definition expects instances of classes that extend BaseGreeter, not the classes themselves that extend it. Is that correct? If that is the case, how do I define an array of classes that extend BaseGreeter?

This is the code:

class BaseGreeter {
    greeting: string;
    greeting_start: string;
    constructor(message: string) {
        this.greeting = message;
    }
    greet() {
        return this.greeting_start + ", " + this.greeting;
    }
}

class Greeter extends BaseGreeter{

    constructor(message: string) {
        super(message);
        this.greeting_start = "Hello";
    }
}

class Greeter2 extends BaseGreeter {
    constructor(message: string) {
        super(message);
        this.greeting_start = "Bye";
    }
}

class Greeter3 extends BaseGreeter {
    constructor(message: string) {
        super(message);
        this.greeting_start = "Adieu";
    }
}


function getGreeters() :[string, BaseGreeter[]]{
    let greeters = new Array<BaseGreeter>();
    greeters.push(Greeter);
    greeters.push(Greeter2);
    greeters.push(Greeter3);
    return ["test", greeters];
}

let foo = {};
let retval = getGreeters();
alert(retval[0]);
foo['greeters'] = retval[1];
var i = 0;

let button = document.createElement('button');
button.textContent = "Say Hello";
button.onclick = function () {
    let greeter = new foo['greeters'][i]("cruel world");
    alert(greeter.greet());
    i = i + 1;
}

document.body.appendChild(button);
like image 507
Dolf Andringa Avatar asked Apr 24 '18 05:04

Dolf Andringa


People also ask

How do you declare an array of types in TypeScript?

In TypeScript, an array is an ordered list of values. An array can store a mixed type of values. To declare an array of a specific type, you use the let arr: type[] syntax.

How array is defined in TypeScript interface?

To define an interface for an array of objects, define the interface for the type of each object and set the type of the array to be Type[] , e.g. const arr: Employee[] = [] . All of the objects you add to the array have to conform to the type, otherwise the type checker errors out.

How do you define a class variable in TypeScript?

The type syntax for declaring a variable in TypeScript is to include a colon (:) after the variable name, followed by its type. Just as in JavaScript, we use the var keyword to declare a variable. Declare its type and value in one statement.

How do you define a number array in TypeScript?

To declare an array of numbers in TypeScript, set the type of the array to number[] , e.g. const arr: number[] = [] . If you try to add a value of any other type to the array, the type checker would show an error. Copied!


1 Answers

BaseGreeter type means that a value conforms to BaseGreeter interface and is an instance of BaseGreeter class. The type that designates BaseGreeter constructor is typeof BaseGreeter:

function getGreeters(): [string, typeof BaseGreeter[]]{
    let greeters = new Array<typeof BaseGreeter>();
    greeters.push(Greeter);
    greeters.push(Greeter2);
    greeters.push(Greeter3);
    return ["test", greeters];
}
like image 183
Estus Flask Avatar answered Nov 28 '22 11:11

Estus Flask