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

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

  for (const item of items) {
    currentChunk.push(item);

    if (currentChunk.length === size) {
      result.push(currentChunk);
      currentChunk = [];
    }
  }

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

  return result;
}
```