```ts
/**
 * 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.
 * @param size The maximum number of elements in each chunk. Must be a positive integer.
 */
export function chunk<T>(items: readonly T[], size: number): T[][] {
    if (typeof size !== 'number' || size <= 0 || size % 1 !== 0) {
        throw new Error('Size must be a positive integer.');
    }

    const result: T[][] = [];
    let start = 0;

    while (start < items.length) {
        const end = Math.min(start + size, items.length);
        result.push(items.slice(start, end));
        start += size;
    }

    return result;
}
```