```ts
/**
 * Flattens a nested array up to a specified depth level.
 * A string is treated as a value and is not iterated into.
 * Depth of 0 returns a copy of the input unchanged. Input must not be mutated.
 */
export function flatten(items: readonly unknown[], depth = Infinity): unknown[] {
  if (typeof depth !== 'number' || depth < 0) {
    throw new Error('Depth must be a non-negative integer or Infinity.');
  }

  return items.reduce((acc, item) => {
    if (item instanceof Array && depth > 0) {
      acc.push(...flatten(item as unknown[], depth - 1));
    } else if (item !== null && item !== undefined) {
      acc.push(item);
    }
    return acc;
  }, []);
}
```