title: Recursive / hierarchy transform
body: <p>I have a function that takes an object with a children-array, of the same type.  Now I want to convert the list of children to a an object indexed by the children's ID.</p>
  
  <p>I'd like to model this in Typescript, and do the following:</p>
  
  <pre class="lang-js prettyprint-override"><code>export const convertHierarchyToDiffFriendly = &lt;T extends {id: string, children?: T[]}, U extends T &amp; {children: {[id: string]: U}}&gt;(x: T): U =&gt; (&lt;U&gt;{
      ...x,
      children: x.children ? 
          &lt;U["children"]&gt;x.children.reduce((z, y) =&gt; ({
              ...z,
              [y.id]: convertHierarchyToDiffFriendly(y)}),
              &lt;{[id: string]: U}&gt;{}) : {}
  })
  </code></pre>
  
  <p>This works, but:</p>
  
  <pre class="lang-js prettyprint-override"><code>const convertedRhs = convertHierarchyToDiffFriendly(&lt;IVendorStatusInput&gt;rhs)
  const c = convertedRhs.children["10"] // c's type is {}, not T &amp; {children: {[id]: U}}
  </code></pre>
  
  <p>It's kind of obvious that U won't be derived correctly, but how could it be?</p>