```typescript
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[] = [];
  const stack: [unknown, number][] = items.map(([item]) => ([item, depth]));

  while (stack.length > 0) {
    const [current, currentDepth] = stack.pop()!;
    
    if (Array.isArray(current)) {
      for (let i = current.length - 1; i >= 0; i--) {
        stack.push([current[i], currentDepth - 1]);
      }
    } else if (depth === Infinity || currentDepth > 0) {
      result.push(current);
    }
  }

  return result;
}
```