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

  return items.reduce((acc, item) => {
    if (Array.isArray(item)) {
      if (depth > 1) {
        acc.push(...flatten(item, depth - 1));
      } else {
        acc.push(...item);
      }
    } else if (typeof item === 'object' && item !== null) {
      if (acc.some(key => key === item)) {
        // Skip duplicate objects to avoid infinite recursion
        return acc;
      }
      acc.push({ ...item });
    } else {
      acc.push(item);
    }
    return acc;
  }, []);
}

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

  return items.reduce((acc, item) => {
    if (Array.isArray(item)) {
      if (depth > 1) {
        acc.push(...flatten(item, depth - 1));
      } else {
        acc.push(...item);
      }
    } else if (typeof item === 'object' && item !== null) {
      if (acc.some(key => key === item)) {
        // Skip duplicate objects to avoid infinite recursion
        return acc;
      }
      acc.push({ ...item });
    } else {
      acc.push(item);
    }
    return acc;
  }, []);
}
```