title: Typescript - factory pattern
body: <p>I'm trying to create factory for <code>MainType</code>. I would also like to reuse already created types (actually I need same instance) so I store them within <code>ItemFactory</code>.</p>
  
  <pre><code>class BaseType {
  
  }
  
  class MainType extends BaseType {
  
  }
  
  class ItemFactory {
      items: { [type: string]: BaseType } = {};
  
      get&lt;T extends BaseType&gt;(type: string): T | null {
          let item = this.items[type];
  
          if (!item) {
              switch (type) {
                  case "main-type":
                      item = new MainType();
                      break;
                  default:
                      return null;
              }
  
              this.items[type] = item;
          }
  
          return item as T;
      }
  }
  </code></pre>
  
  <p>Is there a way to simplify call </p>
  
  <pre><code>itemFactory.get&lt;MainType&gt;("main-type"); // current call
  
  // option 1
  const resolvedType = itemFactory.get&lt;MainType&gt;();
  
  // option 2
  const resolvedType = itemFactory.get("main-type");
  </code></pre>
  
  <p>I would like to have either option 1 or option 2 (no need for both), so I don't have to pass both identifier and type to have correctly resolved resulting type.</p>