```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.
 *
 * @param items - The input array to be chunked.
 */
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 chunks = [];
    for (let i = 0; i < items.length; i += size) {
        const chunk = items.slice(i, i + size);
        if (chunk.length > 0) { // add the full last group otherwise discard
            chunks.push(chunk);
        }
    }
    return chunks;
}
```