```ts
export function flatten(items: readonly unknown[], depth?: number): unknown[] {
    if (typeof depth !== 'number') {
        throw new Error('depth must be a non-negative integer');
    }

    return items.reduce((acc, item) => {
        if (!Array.isArray(item)) {
            acc.push(item);
        } else {
            if (depth === 0) {
                acc.push(...item);
            } else {
                acc.push(...flatten(item, depth - 1));
            }
        }
        return acc;
    }, []);
}
```