TypeScript:有没有办法使用typeof运算符获得类型数组?

Pau*_* Go 2 arrays typeof typescript

给出以下代码:

class Type
{
    static Property = 10;
}

class Type1 extends Type
{
    static Property = 20;
}

class Type2 extends Type
{
    static Property = 30;
}
Run Code Online (Sandbox Code Playgroud)

我想创建一个函数,它可以返回一个类型数组,这些类型都继承自同一个库,允许访问类的"静态方".例如:

function GetTypes(): typeof Type[]
{
    return [Type1, Type2];
}
Run Code Online (Sandbox Code Playgroud)

所以现在理想情况下我可以去:

GetTypes(0).Property; // Equal to 20
Run Code Online (Sandbox Code Playgroud)

但是,似乎没有用于在数组中存储多种类型的语法.

它是否正确?

tho*_*aux 5

当然有.您的代码是正确的减去GetTypes函数的返回类型.(要明确史蒂夫的答案也会解决你的问题,这只是另一种不使用接口的方法).

GetTypes函数的返回类型更改为:

function GetTypes(): Array<typeof Type>
{
    return [Type1, Type2];
}
Run Code Online (Sandbox Code Playgroud)

这应该是诀窍.