```ts
/**
 * Flattens a nested array up to a specified depth.
 * @param items - The array to flatten.
 * @param depth - The maximum depth to flatten. Defaults to Infinity.
 * @returns A new flattened array.
 * @throws Error if depth is not a non-negative integer or Infinity.
 */
export function flatten(items: readonly unknown[], depth: number = Infinity): unknown[] {
  if (typeof depth !== 'number' || !isFinite(depth) && depth !== Infinity || depth < 0) {
    throw new Error('Depth must be a non-negative integer or Infinity.');
  }

  return items.reduce((acc, item) => {
    if (Array.isArray(item) && depth > 0) {
      return acc.concat(flatten(item, depth - 1));
    } else {
      return acc.concat(item);
    }
  }, [] as unknown[]);
}
```