```ts
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 currentGroup: T[] = [];

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

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

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

    return result;
}
```