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

  const result: unknown[] = [];
  let currentDepth = 0;

  const isIterable = (item: unknown): item is Iterable<unknown> =>
    typeof item === "object" && item != null && Symbol.iterator in item;

  function recursiveFlatten(item: unknown, currentDepth: number) {
    if (currentDepth > depth) return;
    if (Array.isArray(item)) {
      for (const subItem of item) {
        recursiveFlatten(subItem, currentDepth + 1);
      }
    } else if (isIterable(item)) {
      for (const [key, value] of Object.entries(item)) {
        recursiveFlatten(value, currentDepth + 1);
      }
    } else {
      result.push(item);
    }
  }

  recursiveFlatten(items, currentDepth);

  return [...result];
}
```