title: Typescript: Using a tuple as index type
body: <p>Given is a tuple of some keys like <code>["a", "b", "c"]</code> and a nested object with those keys as properties <code>{a: {b: {c: number}}}</code>. How do you recursively use the members of the tuple as index in typescript?</p>
  
  <p>A implementation without proper typing:</p>
  
  <pre><code>function recursivePluck(ob: any, tuple: any[]): any {
    for (let index of tuple) {
      ob = ob[index]
    }
    return ob
  }
  </code></pre>
  
  <p>How do you type the above code?</p>
  
  <p>I have tried the following</p>
  
  <pre><code>type RecursivePluck&lt;
    Tuple extends string[], 
    Ob extends {[key in string]: any}, 
    TupleWithoutFirst extends SliceStartQuantity&lt;Tuple, 1&gt; = SliceStartQuantity&lt;Tuple, 1&gt;
  &gt;
  
  = TupleWithoutFirst extends [] ? Ob[Tuple[0]] : RecursivePluck&lt;TupleWithoutFirst, Ob[Tuple[0]]&gt;
  </code></pre>
  
  <p>But this errors <code>Type alias 'RecursivePluck' circularly references itself.</code></p>
  
  <blockquote>
    <p>Note that <code>SliceStartQuantity</code> is from <code>typescript-tuple</code> (<a href="https://www.npmjs.com/package/typescript-tuple" rel="nofollow noreferrer">npm</a>)</p>
  </blockquote>