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

    const result: unknown[] = [];

    function flattenHelper(currentItems: readonly unknown[], currentDepth: number): void {
        for (const item of currentItems) {
            if (Array.isArray(item) && currentDepth < depth) {
                flattenHelper(item, currentDepth + 1);
            } else {
                result.push(item);
            }
        }
    }

    flattenHelper(items, 0);
    return result;
}
```