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

  return items.flatMap((item, index) => {
    const currentDepth = depth === undefined ? 0 : depth;
    if (currentDepth <= 0 || typeof item !== 'object' || item == null) {
      return item;
    }
    if (Array.isArray(item)) {
      return flatten(item, currentDepth - 1);
    } else {
      return [item];
    }
  });
}
```