我有一个返回类的函数。类在函数中定义,定义取决于函数的输入。我正在使用 typescript mixins 。我创建了一个 typescript playground 来清楚地说明问题。
function MIXIN1<TBase extends Mixin1Dependencies>(Base: TBase){
return class MIXIN1Class extends (Base as Mixin1Dependencies){
constructor(props: any){
super(props);
console.log('MXIN1');
}
}
}
type Mixin2Dependencies = MixinDependencies<Origin & MIXIN1Class>;
我试图在 MIXIN1Class
的定义中使用 Mixin2Dependencies
类型定义。有没有办法暴露内部 MIXIN1Class
?或实现类似的行为,我曾尝试将 implements
与单独的接口一起使用,但变得非常多余。
答案 0 :(得分:1)
我认为您正在寻找的是 ReturnType
实用程序类型。
尝试 ReturnType<typeof MIXIN1>
,这应该会导致您的示例中的 MIXIN1Class
。
见https://www.typescriptlang.org/docs/handbook/utility-types.html#returntypetype