【问题标题】:Typescript -- instantiate child from a static method in base class, with args and referencing other static methodTypescript - 从基类中的静态方法实例化子类,使用 args 并引用其他静态方法
【发布时间】:2021-05-03 22:22:00
【问题描述】:

我正在尝试从基类中的静态方法实例化子类。我想正确键入我的基类,而不是使用 any 作为我所有静态方法的返回类型。我尝试了解决方案here,但它不适用于引用其他静态方法或接受参数的静态方法。如何正确地从 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[]) =&gt; T'.(在find 方法中)
  2. Type 'BaseModel' is missing the following properties from type 'Child': name.(在findOne方法的返回类型中)

【问题讨论】:

    标签: node.js typescript typescript-generics typescript-class


    【解决方案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');
    

    【讨论】:

    • 我试图简化实现以使问题清晰。我正在寻找的答案是如何让类型与我的实现一起工作,因为我的实现适用于普通 JS。
    • 我需要能够调用this.getName,因为我的“现实生活”实现取决于子类的名称
    • 我没有将 id 和 name 标记为可选,因为它们不是可选的。
    • @JeremyGottfried 好的,我明白了。我编辑了答案,希望我能正确理解你。 Steel 嘲笑了一些东西(因为我不知道从哪里获取 name、id、db 以及 GetResult 是什么,但现在它使用 child getName。现在看起来还可以吗?
    • 谢谢,看来这行得通!我只是对此感到困惑:this: { new (arg: GetResult): T; getName: () =&gt; string }。为什么我不能使用T extends BaseClass?如果 T 从 BaseClass 扩展,那不保证它有 getName 方法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-24
    • 2023-03-07
    • 1970-01-01
    • 2014-10-04
    • 1970-01-01
    相关资源
    最近更新 更多