title: TypeScript: Infer type of nested union type
body: <p>I'm using objects in TypeScript that can potentially contain a lazy link where the data can be fetched or the data itself. That's why these properties get a union type T | string.</p>
  
  <p>Now I want to write a type safe resolve function to resolve nested properties that returns the nested type.</p>
  
  <pre><code>type TypeOrString&lt;T&gt; = T | string;
  
  
  interface A {
      propA: TypeOrString&lt;B&gt;;
  }
  
  interface B {
      propB: TypeOrString&lt;any[]&gt;;
  }
  
  
  function resolve&lt;T, K1 extends keyof T, K2 extends keyof T[K1]&gt;
  (data: T | string, p1: K1, p2: K2): T[K1][K2] {
      return null;
  }
  
  let a:A;
  let b:TypeOrString&lt;any[]&gt; = resolve(a, "propA", "propB");
  
  //Error TS2345: Argument of type '"propB"' is not assignable 
  //to parameter of type '"toString" | "valueOf"'.
  </code></pre>
  
  <p>But the compiler gives me an error, TS2345: Argument of type '"propB"' is not assignable to parameter of type '"toString" | "valueOf"'. Is there any chance to infer the type of propB from an instance of interface A?</p>