```typescript
/**
 * Tiered cost calculation: first rate up to allowance, second rate beyond.
 */
export function tierCost(
  units: number,
  allowance: number,
  firstRate: number,
  laterRate: number
): number {
  if (units < 0) {
    throw new Error('units must be non-negative');
  }
  if (allowance < 0) {
    throw new Error('allowance must be non-negative');
  }
  if (firstRate < 0 || laterRate < 0) {
    throw new Error('rates must be non-negative');
  }

  return units <= allowance
    ? units * firstRate
    : allowance * firstRate + (units - allowance) * laterRate;
}
```