```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 chunked.
 * @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[][] {
    // Check if size is a positive integer
    if (typeof size !== 'number' || size <= 0) {
        throw new Error('Size must be a positive integer');
    }

    const result: T[][] = [];
    for (let i = 0; i < items.length; i += size) {
        // Create a slice of the array from the current index to the minimum of
        // the current index plus size and the length of the array
        result.push(items.slice(i, Math.min(i + size, items.length)));
    }
    return result;
}
```