【发布时间】:2022-01-26 01:48:09
【问题描述】:
我正在尝试编写一个返回任意数量其他类型的交集的类型。类似的东西
type TypeofStatementIntersections<CL extends Function[]> = { [SN in keyof CL]: (builder: CL[SN]) => CL[SN] }[number] extends
(arg1: infer IT) => void ? IT : never;
function MixStaticReferencesOf<T1 extends Function[]>(...builder: T1): TypeofStatementIntersections<T1> {
return Object.assign({});
}
class C1 {
p1 = 1;
static staticProp1: string = `class property defined in ${this.name}`;
static staticMethod1() {
}
}
class C2 {
static staticProp2: string = `class property defined in ${this.name}`;
static staticMethod2() {
return C2.staticProp2;
}
method2() {
return C2.staticProp2;
}
}
class C3 {
static staticProp3: string = `class property defined in ${this.name}`;
static staticMethod3() {
}
}
const MixedStaticReferences = MixStaticReferencesOf(C1, C2, C3); // : typeof C1 & typeof C2 & typeof C3
它可以混合静态引用,但我想要另一种类型来引用实例混合。我想定义一个返回C1 & C2 & C3 & ...的类型,例子定义了同伴返回类型typeof C1 & typeof C2 & ...
有可能吗?
【问题讨论】:
-
为什么
typeof C1 & typeof C2不正确?您想混合静态。typeof T表示类的静态侧T,T是实例类型。 -
@TitianCernicova-Dragomir 抱歉,我更新了帖子。我想要另一种类型来引用实例混合而不是静态引用。
标签: typescript