0

我正在尝试从基类中的静态方法实例化一个子类。我想正确键入我的基类,而不是any在我所有的静态方法上使用它作为返回类型。我在这里尝试了解决方案,但它不适用于引用其他静态方法或接受参数的静态方法。如何正确地从 typescript 中的基类继承,并且仍然引用其他方法并接受参数?

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.(在findOne方法的返回类型中)
4

1 回答 1

1

find基类的方法中,您应该指定它期望子类实现静态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');
于 2021-05-03T22:38:16.283 回答