```typescript
/**
 * Splits an array into consecutive groups of at most specified size elements.
 * @param items - The input array to be chunked.
 * @param size - The maximum size of each chunk.
 * @returns An array of chunks, where each chunk contains up to `size` elements.
 */
export function chunk<T>(items: readonly T[], size: number): T[][] {
    if (typeof size !== 'number' || size <= 0 || !Number.isInteger(size)) {
        throw new Error('Size must be a positive integer');
    }
    if (items.length === 0) return [];
    const result: T[][] = [];
    for (let i = 0; i < items.length; i += size) {
        result.push(items.slice(i, i + size));
    }
    return result;
}
```