```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 to be split.
 * @param size - The maximum number of elements per chunk.
 * @returns An array of arrays, where each inner array is a chunk of the original array.
 */
export function chunk<T>(items: readonly T[], size: number): T[][] {
    if (typeof size !== 'number' || !Number.isInteger(size) || size <= 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 = end;
    }

    return result;
}
```