```ts
/**
 * Flattens a given 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 up to the specified depth.
 */
export function flatten(items: readonly unknown[], depth = Infinity): unknown[] {
  if (depth < 0 || !Number.isInteger(depth)) {
    throw new Error('Depth must be a non-negative integer or Infinity');
  }

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