```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.
 */
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');
    }

    return items.length === 0
        ? []
        : Array(Math.trunc(items.length / size)).fill(0).map((_, i) => {
            const start = i * size;
            const end = Math.min(start + size, items.length);
            return items.slice(start, end);
        });
}
```