```ts
/**
 * The factorial of n.
 *
 * @throws {Error} If n is negative or non-integer.
 */
export function factorial(n: number): number {
  if (n < 0 || !Number.isInteger(n)) {
    throw new Error('Factorial is not defined for negative numbers or non-integers.');
  }
  if (n === 0) {
    return 1;
  }
  return n * factorial(n - 1);
}
```