TypeScript-基于保存构造函数引用的变量定义类型
是否有可能获得一种保存对构造函数/类引用的变量类型 我正在做:TypeScript-基于保存构造函数引用的变量定义类型,typescript,metaclass,typeof,type-definition,Typescript,Metaclass,Typeof,Type Definition,是否有可能获得一种保存对构造函数/类引用的变量类型 我正在做: const componentUnderTest = MyComponent; type TComponentUnderTest = MyComponent; 我试图通过尝试从测试中的const component“提取”类型来删除重复的MyComponent: const componentUnderTest = MyComponent; type TComponentUnderTest = typeof componentUn
const componentUnderTest = MyComponent;
type TComponentUnderTest = MyComponent;
我试图通过尝试从测试中的const component“提取”类型来删除重复的MyComponent
:
const componentUnderTest = MyComponent;
type TComponentUnderTest = typeof componentUnderTest; // note my attempt here
但是我犯了一个错误
但是,由于相关构造起作用:
const componentUnderTest: MyComponent = null;
type TComponentUnderTest = typeof componentUnderTest; // typeof works here
因此,我的Java直觉表明它应该是这样的:
type TComponentUnderTest = Type<typeof componentUnderTest>;
type TComponentUnderTest=类型;
或
类型TComponentUnderTest=构造函数;
或
类型TComponentUnderTest=Class;
但是我不确定TypeScript中是否存在这样一个通用的元类类型
有可能表达这样一件事吗
这与类似,只是我想引用构造函数/类/类型(const componentUnderTest=MyComponent
)。也尝试了:type tcomonentundertest=typeof(typeof componentUnderTest)但是这会导致一个错误。嗯,也许我需要的是一个“reverse-typeof”操作符。它存在吗?
type TComponentUnderTest = Constructor<typeof componentUnderTest>;
type TComponentUnderTest = Class<typeof componentUnderTest>;
type MyConstructor = new (...args: any[]) => MyComponent
let x: MyConstructor
let y = new x()