```typescript
/** The factorial of n. */
export function factorial(n: number): number {
  if (n === -1) throw new Error("Cannot compute factorial for negative numbers");
  if (!Number.isInteger(n)) throw new Error("Factorial is only defined for integers");

  if (n === 0) return 1;
  
  let result = 1;
  for (let i = 2; i <= n; i++) {
    result *= i;
  }
  return result;
}
```