```ts
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");
  }

  const result: unknown[] = [];

  const recurse = (currentItems: readonly unknown[], currentDepth: number) => {
    for (const item of currentItems) {
      if (Array.isArray(item) && currentDepth > 0) {
        recurse(item, currentDepth - 1);
      } else {
        result.push(item);
      }
    }
  };

  recurse(items, depth);
  return result;
}
```