```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 if not provided.
 * @returns A new flattened array.
 */
export function flatten(items: readonly unknown[], depth: number = Infinity): unknown[] {
  if (!Number.isInteger(depth) || depth < 0) {
    throw new Error("Depth must be a non-negative integer or Infinity.");
  }

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