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