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

  if (typeof items !== 'object' || items === null || Array.isArray(items)) {
    return [...items]; // Return a shallow copy if input is not an array or object
  }

  const result: unknown[] = [];
  Object.values(items).forEach(item => {
    if (depth > 0 && (Array.isArray(item) || typeof item === 'object' && item !== null)) {
      result.push(...flatten(item as unknown[], depth - 1));
    } else {
      result.push(item);
    }
  });

  return result;
}
```