```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): unknown[] {
  if (depth === undefined) depth = Infinity;
  if (typeof depth !== 'number' || depth < 0 && depth !== Infinity) throw new Error('Invalid depth');

  const result: unknown[] = [];
  const stack: { items: readonly unknown[], index: number, currentDepth: number }[] = [{ items, index: 0, currentDepth: 0 }];

  while (stack.length > 0) {
    const { items, index, currentDepth } = stack[stack.length - 1];

    if (index >= items.length || currentDepth === depth) {
      stack.pop();
    } else {
      const item = items[index];
      if (Array.isArray(item)) {
        stack.push({ items: item, index: 0, currentDepth: currentDepth + 1 });
      } else if (typeof item !== 'string') {
        result.push(item);
      }
      stack[stack.length - 1].index++;
    }
  }

  return result;
}
```