如何键入通用递归成员?

时间:2019-09-08 09:01:01

标签: typescript generics

我想定义一个接受静态泛型superComponentClass属性的类,以模仿用Typescript内置类继承无法实现的继承层次结构(因为静态类型是继承的,并且我希望它属于某种类型)或其他)。

我不知道如何键入我的superComponentClass静态属性。这是我的代码:

interface UnknownParams { [key: string]: any };

type ComponentInstance<ParamsType> = {
    params: ParamsType;
}

type UnknownComponentClass = ComponentClass<UnknownParams>;

type ComponentClass<T> = (new (params: T) => ComponentInstance<T>) & {
    superComponentClass?: ComponentClass<UnknownParams>; // Can't use `UnknownComponentClass`, it causes circular reference
};

// Component of Type 1

interface Component1Params {
    foo1: string;
    bar1: string;
}

const myComponent1Constructor: ComponentClass<Component1Params> = class {
    constructor(params: Component1Params) {
        this.params = params;
    }
    params: Component1Params;
}

// Component of Type 2, with static `superComponentClass` is `myComponent1Constructor`.

interface Component2Params {
    foo2: string;
    bar2: string;
}

const myComponent2Constructor: ComponentClass<Component2Params> = class {
    constructor(params: Component2Params) {
        this.params = params;
    }
    params: Component2Params;
    static superComponentClass = myComponent1Constructor;
}

(Playground available)

我发现的(错误)解决方案是将ComponentClass<UnknownParams>类型设置为superComponentClass静态属性,但是TS给了我以下错误:

Type 'typeof myComponent2Constructor' is not assignable to type 'ComponentClass<Component2Params>'.
  Type 'typeof myComponent2Constructor' is not assignable to type '{ superComponentClass?: ComponentClass<UnknownParams> | undefined; }'.
    Types of property 'superComponentClass' are incompatible.
      Type 'ComponentClass<Component1Params>' is not assignable to type 'ComponentClass<UnknownParams> | undefined'.
        Type 'ComponentClass<Component1Params>' is not assignable to type 'ComponentClass<UnknownParams>'.
          Type 'ComponentClass<Component1Params>' is not assignable to type 'new (params: UnknownParams) => ComponentInstance<UnknownParams>'.
            Types of parameters 'params' and 'params' are incompatible.
              Type 'UnknownParams' is missing the following properties from type 'Component1Params': foo1, bar1 – ts(2322)

由于我无法按照@DaGardener's answer in this question中的说明进行操作,因此我尝试将ComponentClass类型的type属性设置为:

type ComponentClass<T, superT> = (new (params: T) => ComponentInstance<T>) & {
    superComponentClass?: ComponentClass<superT, "Err, I dunno what to write here... :c">;
};

但是我不知道什么是父母的父母类型...

也不能这样做:

type UnknownComponentClass = ComponentClass<UnknownParams, UnknownComponentClass>;
// Error : UnknownComponentClass references itself

type ComponentClass<T, parentClass> = (new (params: T) => ComponentInstance<T>) & {
    superComponentClass?: parentClass;
};

使用最后的代码,每当我声明一个新的组件类时,我都需要声明所有的类层次结构:

const myComponent2Constructor: ComponentClass<Component2Params, ComponentClass<Component1Params, undefined>> = class {

(那里只有1个继承级别,但是如果要继承一个深层次的类层次结构呢?那将是一场噩梦。)

那么,如何定义组件类的类型以及可以实现这种继承的superComponentClass呢?

谢谢!

0 个答案:

没有答案