```typescript
/**
 * Splits an array into consecutive groups of at most size elements,
 * preserving order; the final group is shorter when the length does not divide evenly.
 * An empty array yields an empty array. The input array must not be mutated.
 * Throws an Error unless size is a positive integer.
 */
export function chunk<T>(items: readonly T[], size: number): T[][] {
    if (typeof size !== 'number' || size <= 0) {
        throw new Error('size must be a positive integer');
    }

    const result: T[][] = [];
    for (let i = 0; i < items.length; i += size) {
        result.push(Array.from(items.slice(i, i + size)));
    }
    return result;
}
```