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

  const limit = depth === undefined ? Infinity : depth;

  return items.flatMap((item, index) => {
    if (typeof item === "object" && item !== null && !Array.isArray(item)) {
      // If the item is an object and not an array, recursively flatten it
      return Object.values(item).flatMap((value, subIndex) =>
        flatten([value], limit - 1)
      );
    }

    // Return the item itself if it's a primitive or depth is reached
    return { [index]: item };
  }).flat(limit);
}
```