【发布时间】:2019-12-23 20:54:28
【问题描述】:
假设在代码中的某个地方我们有命名空间,由导出-导入例程创建。
namespace ClassCollection {
export class Test1 {
public Method1() { return 1; }
}
export class Test2 {
public Method2() { return 2; }
}
}
在其他地方,比如说在主模块中,我们想要定义代表这些类实例字典的变量。它意味着通过循环遍历命名空间元素来自动生成,但最后看起来像这样:
const collection = {
Test1: new ClassCollection.Test1,
Test2: new ClassCollection.Test2,
}
// to be accessible like this
collection.Test1.Method1();
collection.Test2.Method2();
很容易分别为每个类求解,如下所示:
const Test1: InstanceType<(typeof ClassCollection)["Test1"]>
= new ClassCollection.Test1;
const Test2: InstanceType<(typeof ClassCollection)["Test2"]>
= new ClassCollection.Test2;
Test1.Method1();
Test2.Method2();
但是如何为此进行泛型类型解析呢?我最好的尝试是:
type TypeResolution<T> = {
[C in keyof T]: InstanceType<T[C]>;
};
/** won't work because of:
* Type 'T[C]' does not satisfy the constraint 'new (...args: any) => any'.
*/
const collection: TypeResolution<typeof ClassCollection> = {
Test1: new ClassCollection.Test1,
Test2: new ClassCollection.Test2,
};
【问题讨论】:
标签: typescript