```typescript
export function flatten(items: readonly unknown[], depth?: number): unknown[] {
  if (depth !== undefined && !Number.isInteger(depth) && depth !== Infinity) {
    throw new Error("Depth must be a non-negative integer or Infinity.");
  }

  const defaultDepth = depth === undefined ? Infinity : depth;

  return items.flatMap((item, index) => {
    if (Array.isArray(item)) {
      if (depth > 0) {
        return flatten(item, depth - 1);
      }
      return item;
    }
    return [item];
  });
}
```