I have come across this permutation function in the js-combinatorics library that I would like to customize.
const _BI = typeof BigInt == 'function' ? BigInt : Number;
const _crop = (n) => n <= Number.MAX_SAFE_INTEGER ? Number(n) : _BI(n);
function permutation(n, k) {
if (n < 0)
throw new RangeError(`negative n is not acceptable`);
if (k < 0)
throw new RangeError(`negative k is not acceptable`);
if (0 == k)
return 1;
if (n < k)
return 0;
[n, k] = [_BI(n), _BI(k)];
let p = _BI(1);
while (k--)
p *= n--;
return _crop(p);
}
TypeScript gives me an error in the line p *= n--;
Operator '*=' cannot be applied to types 'number | bigint' and 'number'.
Is there a way to resolve this issue?