title: Infer generic type from key of object
body: <p>My question is a bit more complex so here are possibly all the required parts:</p>
  
  <pre><code>// Common interface with an id and a string lieral type
  interface IHandler {
      id: Id&lt;IHandler&gt;;
      type: string;
  }
  
  // Base class for all the handler, with generic argument providing string literal type and access to inheriting type
  class Base&lt;Type extends string, T extends Base&lt;Type, T&gt;&gt; implements IHandler {
      id: Id&lt;T&gt; = Guid.raw() as any
  
      // In consturctor string literal type is instantiated
      protected constructor(public type: Type) {
          this.type = type;
      }
  
      // Static function that retrieves handler by it's key from a store
      static GetHandler: &lt;T extends IHandler&gt;(get: Id&lt;T&gt;) =&gt; T;
  
      // Getter that accepts key of an id attribute and returns instance from store
      get = &lt;Handler extends Base&lt;Handler["type"], Handler&gt;&gt;(key: HandlerIds&lt;T, Handler&gt;) =&gt; Base.GetHandler&lt;Handler&gt;((this as any)[key]);
  }
  
  // Keys of attributes that are Id's
  type HandlerIds&lt;T extends Base&lt;T["type"], T&gt;, U extends Base&lt;U["type"], U&gt;&gt; = keyof SubType&lt;Model&lt;T&gt;, Id&lt;U&gt;&gt;;
  
  // Creates a subtype based on condition
  type SubType&lt;Base, Condition&gt; = Pick&lt;Base, { [Key in keyof Base]: Base[Key] extends Condition ? Key : never }[keyof Base]&gt;;
  
  // Excludes attributes of Base
  type Model&lt;T extends Base&lt;T["type"], T&gt;&gt; = Partial&lt;Pick&lt;T, Exclude&lt;keyof T, keyof Base&lt;T["type"], T&gt;&gt;&gt;&gt;;
  
  // Type that holds guid reference and also a type that guid is supposed to be pointing to
  type Id&lt;T extends IHandler&gt; = string &amp; { __type: T }
  </code></pre>
  
  <p>So what I have is an <code>interface</code>, then a <code>base</code> class that implements a <code>getter</code>, and then derived classes that pass their type to the base class. Also there is an <code>Id</code> type the holds unique identifier of handler as well as it's type. Each handler has it's own id in <code>id</code> variable and then can save references to other handlers in attribute of type <code>Id</code> with generic argument of that handler's type.</p>
  
  <p>What I would like to make work is to properly type the <code>get</code> function so it infers the type of handler it is getting by provided <code>key</code>. Since each key is templated by type of handler it is pointing to, this type should be probably used to infer the return type, but with setup like this it does not work.</p>
  
  <p>You can see an example of desired usage here:</p>
  
  <pre><code>class Foo extends Base&lt;"Foo", Foo&gt; {
      a: Id&lt;Goo&gt;;
      b: Id&lt;Foo&gt;;
      constructor() {
          super("Foo");
          // Get a reference to instance of "a" of type Goo
          var aHandler = this.get("a");
          // Get a reference to instance of "b" of type Foo
          var bHandler = this.get("b");
      }
  }
  class Goo extends Base&lt;"Goo", Goo&gt; {
      constructor() {
          super("Goo");
      }
  }
  </code></pre>
  
  <p>What I would like to achieve here is for the attributes <code>aHandler</code> and <code>bHandler</code> to be automatically inferred as <code>Foo</code> and <code>Goo</code>.</p>