```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.
 * @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' ||isNaN(size) || size <= 0) {
        throw new Error('Size must be a positive integer');
    }

    const result: T[][] = [];
    let currentChunk: T[] = [];

    for (const item of items) {
        if (currentChunk.length >= size) {
            result.push(currentChunk);
            currentChunk = [item];
        } else {
            currentChunk.push(item);
        }
    }

    if (currentChunk.length > 0) {
        result.push(currentChunk);
    }

    return result;
}
```