```typescript
export function flatten(items: readonly unknown[], depth?: number): unknown[] {
    /**
     * Returns a new array with nested arrays flattened up to depth levels,
     * defaulting to every level. A string is a value, never something to iterate into,
     * no matter how deep it appears. A depth of 0 returns a copy of the input unchanged.
     *
     * @param items - The nested array to flatten
     * @param depth - The maximum depth to flatten (default: Infinity)
     */
    if (depth === undefined) {
        depth = Infinity;
    }
    if (typeof depth !== 'number' || depth < 0) {
        throw new Error('Invalid depth');
    }

    const result: unknown[] = [];
    for (const item of items) {
        if (Array.isArray(item)) {
            if (depth > 1) {
                result.push(...flatten(item, depth - 1));
            } else {
                result.push(item);
            }
        } else {
            result.push(item);
        }
    }
    return result;
}
```