title: Extract Type from Anonymous Generic TypeScript Class
body: <p>I can't seem to be able to determine the most elegant way to determine the type of anonymous generic classes that are generated and returned from functions.</p>
  
  <hr>
  
  <p>Here is a snippet that contains the function which generates the anonymous classes, and one of the functions contained within.</p>
  
  <p>The generics are needed in order for TypeScript to provide return type and parameter property hints when using the get function.</p>
  
  <pre class="lang-js prettyprint-override"><code>export function createController&lt;T, U extends { id?: number }&gt;(cls: new () =&gt; T) {
      return class extends BaseController {
          public constructor() {
            super(cls.name.split(/(?=[A-Z])/).join('_').toLowerCase());
          }
  
          public async get(params: U): Promise&lt;T | null&gt; {
            try {
              const { data } = params.id ? await this.http.get(`${this.base}-${params.id}`) : await this.http.get('', { params });
              return deserialize(cls, search(data, 'data'));
            } catch (err) {
              return null;
            }
          }
      };
  }
  
  </code></pre>
  
  <p>The problem occurs when I try to actually create and store the result of this.</p>
  
  <hr>
  
  <p>Here is an example to create a UserController.</p>
  
  <pre class="lang-js prettyprint-override"><code>export interface UserQueryParams {
      id?: number;
      username?: string;
  }
  
  const UserController = createController&lt;User, UserQueryParams&gt;(User);
  </code></pre>
  
  <p>Now, UserController instances can be created by simply creating them like normal classes through <code>new UserController()</code>;</p>
  
  <p>There isn't a problem when storing them directly, i.e </p>
  
  <pre class="lang-js prettyprint-override"><code>// Correctly inferred as createController&lt;User, UserQueryParams&gt;.(Anonymous class)
  const controller = new UserController();
  </code></pre>
  
  <p>However, say I wanted to create a class containing these controllers:</p>
  
  <pre class="lang-js prettyprint-override"><code>class Client {
      public controller: ???
  
      public constructor() {
          this.controller = new UserController();
      }
  }
  </code></pre>
  
  <p>I can't seem to figure out the best way to type the property.</p>
  
  <p>Using <code>public controller: UserController</code> errors, because <code>UserController</code> is a value and not a type.</p>
  
  <p>Using <code>public controller: typeof UserController</code> also does not appear to work, the contructor assignment then fails because of incompatible types.</p>
  
  <p>Using <code>public controller: typeof UserController.prototype</code> ALMOST works, but the type information is lost. It is inferred as <code>createController&lt;any, any&gt;.(Anonymous class)</code>. The two <code>any</code>'s used for the generics means that the parameter and return type information is gone.</p>
  
  <p>Using </p>
  
  <pre class="lang-js prettyprint-override"><code>const temp = new UserController();
  
  // In the class..
  
  public controller: typeof temp;
  </code></pre>
  
  <p>This works! However it's extremely ugly to have to create temporary instances of EVERY controller I might generate. </p>
  
  <p>Is there any other way to type <code>controller</code> properly?</p>