繁体   English   中英

Typescript:抽象泛型 class 的子类类型

[英]Typescript: type of subclasses of an abstract generic class

我有一个基本的通用 class:

abstract class BaseClass<T> {
  abstract itemArray: Array<T>;

  static getName(): string {
    throw new Error(`BaseClass - 'getName' was not overridden!`);
  }

  internalLogic() {}
}

和继承人:

type Item1 = {
  name: string
}
class Child1 extends BaseClass<Item1> {
  itemArray: Array<Item1> = [];
  static getName(): string {
    return "Child1";
  }
}


type Item2 = {
  name: number
}
class Child2 extends BaseClass<Item2> {
  itemArray: Array<Item2> = [];
  static getName(): string {
    return "Child2";
  }
}

现在我想定义一个 object ,其值是继承者:

type IChildrenObj = {
  [key: string]: InstanceType<typeof BaseClass>;
};

/* 
The following error is received: Type 'typeof BaseClass' does not satisfy the constraint 'new (...args: any) => any'.
  Cannot assign an abstract constructor type to a non-abstract constructor type. ts(2344)
*/

const Children: IChildrenObj = {
  C1: Child1,
  C2: Child2,
}

最后,我希望能够使用子级的静态方法,并且还能够创建它们的实例:

const child: typeof BaseClass = Children.C1;
/*
received the following error: Property 'prototype' is missing in type '{ getName: () => string; }' but required in type 'typeof BaseClass'. ts(2741)
*/

console.log(child.getName());
const childInstance: BaseClass = new child();
/*
The following 2 errors are received:
(1) Generic type 'BaseClass<T>' requires 1 type argument(s). ts(2314)
(2) Cannot create an instance of an abstract class. ts(2511)
Generic type 'BaseClass<T>' requires 1 type argument(s). ts(2314)
*/

首先,类型

type IChildrenObj = {
  [key: string]: InstanceType<typeof BaseClass>; // instances?
};

不适合描述您的Children object。 Children存储class 构造函数,而InstanceType<typeof BaseClass> ,即使它适用于抽象类(正如您所指出的,它不是),也会谈论class 实例 写得更近了

type IChildrenObj = {
  [key: string]: typeof BaseClass; // more like constructors
};

但这也不是Children商店的内容:

const Children: IChildrenObj = {
  C1: Child1, // error!
  // Type 'typeof Child1' is not assignable to type 'typeof BaseClass'.
  // Construct signature return types 'Child1' and 'BaseClass<T>' are incompatible.
  C2: Child2, // error!
  // Type 'typeof Child2' is not assignable to type 'typeof BaseClass'.
  // Construct signature return types 'Child2' and 'BaseClass<T>' are incompatible.
}

类型typeof BaseClass有一个抽象构造签名,类似于new <T>() => BaseClass<T> 调用者(或更有用的是,扩展BaseClass的子类)可以为T选择他们想要的任何东西,并且BaseClass必须能够处理它。 但是typeof Child1 Child1 和typeof Child2类型不能为new Child1()的调用者或扩展器class Grandchild2 extends Child2想要的任何T生成BaseClass<T> Child1只能构造一个BaseClass<Item1> ,而Child2只能构造一个BaseClass<Item2>

所以目前IChildrenObj说它拥有构造函数,每个构造函数都可以为每个可能的类型T生成一个BaseClass<T> 您真正想要的是IChildrenObj说它拥有构造函数,每个构造函数都可以为某些可能的类型T生成BaseClass<T> “every”和“some”之间的差异与类型参数T量化方式之间的差异有关; TypeScript(以及大多数其他具有泛型的语言)仅直接支持“每个”或通用量化。 不幸的是,没有直接支持“一些”或存在量化。 有关开放功能请求,请参阅microsoft/TypeScript#14446

有一些方法可以准确地编码TypeScript 中的存在类型,但除非你真的关心类型安全,否则使用这些方法可能有点烦人。 (但如果需要,我可以详细说明)

相反,我在这里的建议可能是重视生产力而不是完整的类型安全,并且只使用故意松散的any类型来表示您不关心的T


因此,这是定义IChildrenObj的一种方法:

type SubclassOfBaseClass =
  (new () => BaseClass<any>) & // a concrete constructor of BaseClass<any>
  { [K in keyof typeof BaseClass]: typeof BaseClass[K] } // the statics without the abstract ctor

/* type SubclassOfBaseClass = (new () => BaseClass<any>) & {
    prototype: BaseClass<any>;
    getName: () => string;
} */

type IChildrenObj = {
  [key: string]: SubclassofBaseClass
}

SubclassOfBaseClass类型是以下各项的交集: 产生BaseClass<any>实例的具体构造签名 和一个映射类型,它从typeof BaseClass中获取所有 static 成员,而不获取有问题的抽象构造签名。

让我们确保它有效:

const Children: IChildrenObj = {
  C1: Child1,
  C2: Child2,
} // okay

const nums = Object.values(Children)
  .map(ctor => new ctor().itemArray.length); // number[]
console.log(nums); // [0, 0]

const names = Object.values(Children)
  .map(ctor => ctor.getName()) // string[]
console.log(names); // ["Child1", "Child2"]

看起来不错。


这里需要注意的是,虽然IChildrenObj可以工作,但它的类型太模糊,无法跟踪您可能关心的事情,例如Children的特定键/值对,尤其是索引签名的奇怪的“任何事情发生”行为以及BaseClass<any> any的 any :

// index signatures pretend every key exists:
try {
  new Children.C4Explosives() // compiles okay, but
} catch (err) {
  console.log(err); // 💥 RUNTIME: Children.C4Explosives is not a constructor
}

// BaseClass<any> means you no longer care about what T is:
new Children.C1().itemArray.push("Hey, this isn't an Item1") // no error anywhere

因此,在这种情况下,我的建议是仅确保将Children分配给IChildrenObj而无需实际对其进行注释。 例如,您可以使用帮助程序 function:

const asChildrenObj = <T extends IChildrenObj>(t: T) => t;

const Children = asChildrenObj({
  C1: Child1,
  C2: Child2,
}); // okay

现在Children仍然可以在您需要IChildrenObj的任何地方使用,但它仍会记住所有特定的键/值映射,因此当您做坏事时会发出错误:

new Children.C4Explosives() // compiler error!
//Property 'C4Explosives' does not exist on type '{ C1: typeof Child1; C2: typeof Child2; }'

new Children.C1().itemArray.push("Hey, this isn't an Item1") // compiler error!
// Argument of type 'string' is not assignable to parameter of type 'Item1'

如果需要,您仍然可以使用IChildrenObj

const anotherCopy: IChildrenObj = {};
(Object.keys(Children) as Array<keyof typeof Children>)
  .forEach(k => anotherCopy[k] = Children[k]);

Playground 代码链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM