Workaround for accessing class type arguments in static method in Typescript

前端 未结 1 1394
迷失自我
迷失自我 2020-12-16 04:40

The following error

Static members cannot reference class type parameters.

results from the following piece of code



        
相关标签:
1条回答
  • 2020-12-16 04:53

    You could do something like this:

    function Resource<T>() {
      abstract class Resource {
        /* static methods */
        public static list: T[] = [];
        public static async fetch(): Promise<T[]> {
          return null!;
        }
        /*  instance methods */
        public save(): Promise<T> {
          return null!
        }
      }
      return Resource;
    }
    

    In the above Resource is a generic function that returns a locally declared class. The returned class is not generic, so its static properties and methods have concrete types for T. You can extend it like this:

    class Model extends Resource<Model>() {
      // overloading should also work
      public static async fetch(): Promise<Model[]> {
        return super.fetch();
      }
    }
    

    Everything has the types you expect:

     Model.list; // Model[]
     Model.fetch(); // Promise<Model[]>
     new Model().save(); // Promise<Model>
    

    So that might work for you.

    The only caveats I can see right now:

    • There's a bit of duplication in class X extends Resource<X>() which is less than perfect, but I don't think you can get contextual typing to allow the second X to be inferred.

    • Locally-declared types tend not to be exportable or used as declarations, so you might need to be careful there or come up with workarounds (e.g., export some structurally-identical or structurally-close-enough type and declare that Resource is that type?).

    Anyway hope that helps. Good luck!

    0 讨论(0)
提交回复
热议问题