简体   繁体   English

如何在Typescript中继承静态泛型方法

[英]How to inherit static generic methods in Typescript

I can't compile Typescript code because the compiler does not seem to understand the inheritance between my classes. 我无法编译Typescript代码,因为编译器似乎不理解我的类之间的继承。

When I try to compile, I run into this error 当我尝试编译时,我遇到了这个错误

Property 'create' does not exist on type 'new () => T'.

export abstract class Resource {

    // creates a new resource and returns it
    static async create<T>(this: { new(): T }, resource: T): Promise<T> {
      const resource = ... // using "this"
      return resource;
    }

}

export abstract class ContainerResource extends Resource {

  static async addToContainer<T>(this: { new(): T }, resource: T, containerId: string): Promise<T> {
    r = await this.create(resource); // Property 'create' does not exist on type 'new () => T'.

    // do some stuff

    return r;
  }

}

I except this code to compile. 我除了这个代码编译。 It does not work even with addToContainer<T extends Resource> :( 即使使用addToContainer<T extends Resource>它也不起作用:(

You are referencing a static method through "this". 您正通过“this”引用静态方法。 That's always wrong. 这总是错的。 Try replacing "this" with "Resource" 尝试用“资源”替换“this”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM