让我们说您有一个接口,然后类实现了它。
interface A { /*do stuff*/ }
class B implements A { /*do stuff*/ }
class C implements A { /*do stuff*/ }
然后,如果您有一个可以存储类类型的变量,而不是对象的特定实例,那么您将什么作为类型呢?
let x: Something = B; // or C
我知道对于对象可以使用typeof,但对于接口则不能。但是我能够指定变量来存储接口的实现,所以我不明白为什么我不应该存储接口的类。
答案 0 :(得分:1)
您可以使用构造函数签名(类似于函数签名,但前面带有new
)
interface A { /*do stuff*/ }
class B implements A { /*do stuff*/ }
class C implements A { /*do stuff*/ }
let x: new () => A = B;
let x2: new () => A = C;