```ts
/**
 * Flattens a nested array up to a specified depth.
 *
 * @param items - The array to flatten.
 * @param depth - The maximum depth to flatten. Defaults to Infinity.
 * @returns A new flattened array.
 */
export function flatten(items: readonly unknown[], depth: number = Infinity): unknown[] {
    if (!Number.isInteger(depth) || depth < 0) {
        throw new Error("Depth must be a non-negative integer or Infinity.");
    }
    return items.reduce((acc, item) => {
        if (depth > 0 && Array.isArray(item)) {
            acc.push(...flatten(item as readonly unknown[], depth - 1));
        } else if (typeof item !== 'string' && Array.isArray(item)) {
            throw new Error("String encountered inside an array."); // This should never happen
        } else {
            acc.push(item);
        }
        return acc;
    }, []);
}
```