```ts
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 (Array.isArray(item)) {
      const flattenedSubarray = flatten(item as readonly unknown[], depth - 1);
      acc.push(...flattenedSubarray);
    } else {
      acc.push(item);
    }
    return acc;
  }, []);
}
```