```
/** The factorial of n. Throws Error for negative or non-integer arguments. */
export function factorial(n: number): number {
  if (n < 0 || !Number.isInteger(n)) {
    throw new Error(`Invalid argument: ${n} is not a non-negative integer.`);
  }
  if (!isNaN(n) && Number.isFinite(n) && n >= 0) {
    return n === 0 ? 1 : n * factorial(n - 1);
  } else {
    throw new Error(`Invalid argument: ${n} is out of bounds.`);
  }
}
```