typescript - typeof 类或其任何子类,而不是类的实例
问题描述
我有这些课程
class Foo {}
class A extends Foo {}
class B extends Foo {}
现在我想创建一个接口,type
它的类型是类而不是类的实例
interface Bar {
type : XXXX; // I don't know to put here
value : string;
}
所以我可以像这样创建该接口的实例
const x : Bar = {
type : Foo, // or and of its subclasses for example A or B
value : 'test',
}
我不想要一个实例,而是Foo
一个类Foo
{
type : new Foo(),
value : "test"
}
不应该是有效的
我希望我正确地解释了我的问题
解决方案
看起来您只需要form的构造签名new () => Foo
,意思是“产生类型实例的零参数构造函数Foo
”。像这样:
interface Bar {
type: new () => Foo;
value: string;
}
const x: Bar = {
type: Foo,
value: 'test',
};
const y: Bar = {
type: A,
value: ""
};
const z: Bar = {
type: new Foo(), // error!
// ~~~~ <-- // Type 'Foo' is not assignable to type 'new () => Foo'.
value: ""
}
推荐阅读
- ios - 如何让我的应用仅在纵向模式下适合所有 iPhone 尺寸
- laravel - 负载对于 laravel_session 解密无效
- java - 从流下载时出现角度奇怪的行为
- c - 如何调试产生意外输出的 C 程序?
- vb.net - 重定向 Console.Write
- matlab - 在 MATLAB 中使用极坐标图函数有些奇怪
- javascript - 如何将表单提交发布到 URL 并在提交时重定向到不同的 URL
- javascript - Google 脚本触发器仅在周一/周三/周五运行
- dynamics-crm - 如何以编程方式检索在 Dynamics 365 中导入的记录?
- filenames - Talend 获取当前文件名