typeof typescript class 不能分配给扩展它的 class

Typeof typescript class is not assignable to that of a class which extends it

我们有一个基础 StoreObject class,它为 transformations/sanitisation 等对象提供通用方法,以便将对象保存到数据库中。我希望能够使用泛型从这些方法中指定更严格的 return 类型。但是,在将 typeof StoreObjecttypeof AClassThatExtendsStoreObject 进行比较时,我的尝试导致了错误,这是我们在各种实用程序函数中进行的检查。你能指出我在下面做错的方向吗?


class StoreObject<S> {
  toStoreObject(s: S): S {
    // Perform some sanitisation etc.
    return s;
  }
}

interface Fact {
  id: string;
  fact: string;
}

// A concrete implementation of StoreUnit where we attempt to define the actual model of the object being stored
class FactStoreObject extends StoreObject<Fact> {}

// We've got some general utils that interact objects using these classes 
// These typicallay take a concrete implementation of StoreUnit to be used as a parser/processer
function doSomething(StoreObjectClass: typeof StoreObject) {
  // Typically:
  // const parsedObject = new StoreObjectClass(someObject).toStoreObject()
  // persistToDb(parsedObject)
}

// This errors with "Type 'S' is not assignable to type 'Fact'"
doSomething(FactStoreObject);

playground link

错误是由于这一行:

const parsedObject = new StoreObjectClass(someObject)

首先,你需要可构造的界面:

export interface IConstructable<T> {
    new (): T;
}

其次,您需要将类型的参数包含到您的工厂方法中:

function doSomething<T>( t: IConstructable<T> ) {
  const parsedObject = ( new t() ).toStoreObject();
  // persistToDb(parsedObject);
}

在提供的代码中,在“doSomething”函数中,参数类型被指定为“StoreObject”,而没有它正在检查的通用类型。为了修复错误,您需要执行以下操作:

  1. 将“StoreObject”的通用参数(在本例中为Fact)传递给doSomthing

  2. doSomething 函数的参数必须指定具有允许创建新实例的构造函数签名的类型。

    示例如下:

function doSomething<T>(StoreObjectClass: { new(): StoreObject<T> }) {
  // typically
  const someObject = <any>{id: "001", fact: "A real fact"};
  const parsedObject = new StoreObjectClass().toStoreObject(someObject);
  // persistToDb(parsedObject)
}

doSomething<Fact>(FactStoreObject);