title: Returning a Type as a Variable in TypeScript
body: <p>I'm currently writing a class factory in TypeScript, and would like to return a type as the output of a function. Although TypeScript handles types as an input--i.e. generics--beautifully, I've not yet found a way to handle types as an output.</p>
  
  <p><a href="https://stackoverflow.com/questions/52879936/factory-functions-that-return-classes-class-constructors-in-typescript">This StackOverflow question on class factories</a> offered a particularly helpful solution, but did not fully answer my question. Given the below structure,</p>
  
  <pre><code>function factory(someVar: any) {
      return class A {
          // Do something with someVar that makes this class unique
      }
  }
  </code></pre>
  
  <p>the question suggests that one should do the following with the output from this factory function:</p>
  
  <pre><code>import factory from "someLocation";
  
  const AClass = factory({foo: "bar"});
  type A = InstanceType&lt;typeof AClass&gt;;
  
  interface IData {
      someField: A;
  }
  </code></pre>
  
  <p>I'd be interested in including this functionality in my factory function to make the system more reusable or modular. However, as initially stated, I'm unsure of how to return a type from a function. If I attempt the following, TypeScript throws the error, <code>[ts] 'MenuState' only refers to a type, but is being used as a value here. [2693]</code>:</p>
  
  <pre><code>function factory(someVar: any) {
      class AClass {
          // Do something with someVar that makes this class unique
      }
  
      type A = InstanceType&lt;typeof AClass&gt;;
      return A;
  }
  </code></pre>
  
  <p>How might I go about this? In general, is there any semantically correct way to handle types as values or variables? If not, why does this violate best practice in TypeScript? Furthermore, how might one then use this type as a constructor, if possible?</p>