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

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

  function processItem(item: unknown): void {
    if (typeof item === 'object' && item !== null) {
      if (currentDepth >= depth || Array.isArray(item)) {
        for (const element of item as any[]) {
          processItem(element);
        }
      } else {
        result.push(item);
      }
    } else {
      result.push(item);
    }
  }

  items.forEach(processItem);

  return result;
}
```