```typescript
export function flatten<T>(items: readonly T[], depth?: number): T[] {
  if (!Number.isInteger(depth) && depth !== Infinity && !isNaN(depth)) {
    throw new Error("Depth must be a non-negative integer or Infinity.");
  }

  return items.flatMap((item, index) => {
    if (typeof item === 'object' && item !== null && !(item instanceof Array)) {
      // Handle objects by returning an array of values
      return Object.values(item).flatMap((value) =>
        flatten([value], depth - 1)
      );
    } else if (Array.isArray(item)) {
      // Flatten arrays up to the given depth
      return item.flatMap((subItem, subIndex) => {
        const currentDepth = depth === undefined || depth > index ? depth : depth - index - 1;
        return flatten([subItem], currentDepth);
      });
    } else {
      // Return non-iterable values as they are
      return [item];
    }
  });
}
```