Typeof TypeScript类不能分配给扩展它的类

时间:2019-07-08 15:59:55

标签: typescript typescript-typings typescript-generics

我们有一个基础StoreObject类,它为保存到数据库的对象提供了转换/消毒等通用方法。我希望能够使用泛型从这些方法中指定更严格的返回类型。但是,在将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

2 个答案:

答案 0 :(得分:1)

由于此行而导致错误:

const parsedObject = new StoreObjectClass(someObject)

首先,您需要可构造的接口:

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

第二,您需要在工厂方法中包括该类型的参数:

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

答案 1 :(得分:0)

在提供的代码中,在“ 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);