如何在非静态方法中获取静态 属性 - 打字稿

How to get the static property in non static method - typescript

我有与以下代码类似的 classes。

class Base {
  protected abstract static link: string;

  public fetch(){
    // Solution need
    const link =  self.link;

    // Do other things with link
  }
}

class A extends Base {
  protected static link = 'some link'
}

class B extends Base {
 protected static link = 'other link'
}

我需要定义一个通用函数来根据 link 值为每个模型获取数据。 我无法将静态 link 值更改为 属性。由于某些静态方法使用此 link 值。

我试过 Base.link。但是我不能用这种方法访问 child class 的原始值。提前致谢!

class Base {
  protected static link: string;

  public fetch(){
    const self = Object.getPrototypeOf(this).constructor;
    return self.link
  }
}

class A extends Base {
  static link = 'some link'
}

class B extends Base {
  static link = 'other link'
}

const b = new B();
const a = new A();
console.log(b.fetch()) // other link
console.log(a.fetch()) // some link

https://stackblitz.com/edit/typescript-kwsejh