title: How to typecheck polymorphic function in flow?
body: <p>I have trouble with polymorphic functions in Flow.
  </p>
  
  <pre><code>type A = { id: string };
  type B = { id: number };
  
  function takeId(x) {
    return x.id;
  }
  
  const a: A = { id: 'akn2' };
  const b: B = { id: 14 }; 
  
  (takeId(a) : string);
  // Error 
  (takeId(b) : number);
  // Error 
  </code></pre>
  
  
  
  <p>I understand that flow engine will type polymorphic function accordingly to context, and where they have been called.</p>
  
  <p>In this case, I reckon that <code>takeId</code> will have the following type : <code>(A|B) =&gt; (string | number)</code>. That's probably why I can't cast the return to its real type.</p>
  
  <p>Furthermore, the code still doesn't typecheck if I cast <code>takeId</code> with the following type <code>(A =&gt; string) | (B =&gt; number)</code>;</p>
  
  <p>How to make the return of polymorphic function typecheck with Flow, without loosing security (no <code>any</code> for instance) ?</p>
  
  <p>If not possible, what would be an idomatic way of achieving the same result (factorizing <code>takeId</code>) ?</p>
  
  <p>Thanks</p>