title: Force Typescript generic parameters (a: T, B: T) to be of the same type
body: <p>Given:</p>
  <pre><code>function sorter&lt;T extends string | number&gt; () {
    const icf = new Intl.Collator().compare
    return (a: T, b: T) =&gt; typeof a === 'number' ? a - b : icf(a, b)
  }
  </code></pre>
  <p>We get a Typescript type error:</p>
  <p><a href="https://i.stack.imgur.com/07TaQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/07TaQ.png" alt="typescript error" /></a></p>
  <p>We know that if <code>a</code> is a number then <code>b</code> will also be a number, and similarly if <code>a</code> is a string then <code>b</code> will also be a string.  This information is <em>not</em> known by the function calling <code>sorter</code> i.e. only at the time the function being returned is called.  By design we never expect to get heterogeneous items.</p>
  <p>What options are there to assert these types correctly?</p>
  <p><a href="https://www.typescriptlang.org/play?#code/LAKA9AVBoAQTADAhgmSB2ATRAjVSAnAUxgBcALEgZyQFsTSBPABxIEsA6IjmNgM0QpeVNDHQBXWjiIEyldLlQB3NgBtVMaaIlSZHWBDCgiAD2YB7AqRh9x6AMak25hVUukZAHgAqMUx6wRKlICNnQAcxgAHzFJaQIAPhgAClVzeyRVIgAuGGDQiIBKGABvWBh7F2DeewEAXjEiJRgASXRSVQ4AYXN1JFJLVPTMokKOStpmQiJy4lJxAgVkpFzvABpNVeK6pKZWcwEkGDqTmAByHXizmAB+UQBaTRhctlrljZxC0ABfUCA" rel="nofollow noreferrer">Typescript Playground</a></p>