Typescript——使用 args 和引用其他静态方法从基 class 中的静态方法实例化子对象

Typescript -- instantiate child from a static method in base class, with args and referencing other static method

我正在尝试从基 class 中的静态方法实例化子 class。我想正确输入我的基础 class 而不是在我所有的静态方法上使用 any 作为 return 类型。我尝试了 解决方案,但它不适用于引用其他静态方法或接受参数的静态方法。我怎样才能正确地从 typescript 中的基础 class 继承,并且仍然引用其他方法并接受参数?

class BaseClass {
  id: string;

  [key: string]: unknown;

  static getName() {
    return this.name.toUpperCase()
  }

  static async find<T extends BaseClass>(this: new (...args: any[]) => T, id: string)
  : Promise<T> {
    const tableName = this.getName();

    const result: GetResult = db.find(tableName, id);

    return new this(result);
  }
}

class Child extends BaseClass {
  name: string;

  static findOne(id: string): Promise<Child> {
    return this.find(id);
  }
}

Child.find('abcd');

这会导致两个不同的错误

  1. Property 'getName' does not exist on type 'new (...args: any[]) => T'.(在find方法中)
  2. Type 'BaseModel' is missing the following properties from type 'Child': name.(在 return 类型中为 findOne 方法)

在基础 class 的 find 方法中,您应该指定它期望子 class 实现静态 getName 方法,如下所示:

static async find<T>(this: { new (arg: GetResult): T } & typeof BaseClass, id: string): Promise<T>

特别是 { new (arg: GetResult): T } 为您带来了构造函数,typeof BaseClass 为您带来了静态成员。

我模拟了一些遗漏的部分并进行了类型检查。

type GetResult = string;

const db = {
    find: (a: string, b: string) => "bar",
}

class BaseClass {
  id: string = "bzzzz";

  [key: string]: unknown;

  static getName() {
    return 'NAME'
  }

  static async find<T>(this: { new (arg: GetResult): T } & typeof BaseClass, id: string)
  : Promise<T> {
    const tableName = this.getName();

    const result: GetResult = db.find(tableName, id);

    return new this(result);
  }
}

class Child extends BaseClass {
  name: string = "Child";

  static findOne(id: string): Promise<Child> {
    return this.find(id);
  }
}

Child.find('abcd');