```ts
/**
 * Flattens an 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): unknown[] {
    if (depth === undefined) depth = Infinity;
    if (typeof depth !== 'number' || isNaN(depth) || depth < 0) throw Error('Invalid depth');

    return items.reduce((acc, val) => {
        if (Array.isArray(val) && depth > 0) {
            acc.push(...flatten(val as unknown[], depth - 1));
        } else if (typeof val !== 'string') {
            acc.push(val);
        }
        return acc;
    }, [] as unknown[]);
}
```