title: Is it possible to declare a typescript function which works on both numbers and bigints?
body: <p>In plain untyped Javascript, it's not so hard to write a function which can operate on either numbers or bigints, depending on the arguments which are passed in:</p>
  <pre class="lang-js prettyprint-override"><code>const sumOfSquares = (a,b) =&gt; a*a + b*b;
  sumOfSquares(3, 4); // returns 25
  sumOfSquares(3n, 4n); // returns 25n
  sumOfSquares(3n, 4); // throws a TypeError
  </code></pre>
  <p>It seems like there ought to be a way to declare this function in typescript so that the compiler will enforce that the arguments will work together. I tried</p>
  <pre class="lang-js prettyprint-override"><code>const sumOfSquares = &lt;N extends bigint | number&gt;(a: N, b: N): N =&gt;
    a * a + b * b;
  </code></pre>
  <p>But the compiler rejects this:</p>
  <blockquote>
  <p>semantic error TS2322: Type 'number' is not assignable to type 'N'.<br />
    'number' is assignable to the constraint of type 'N', but 'N' could be instantiated with a different subtype of constraint 'number | bigint'.</p>
  </blockquote>
  <p>Is there a different way to write the type declaration so that it will work?</p>