TL;DR
を異なる型の配列でも使えるように型定義しただけです(プルリクだしてみようかな)
type CartesianProductElement<T extends unknown[][]> = T extends []
? []
: T extends [Array<infer V>, ...infer W extends unknown[][]]
? [V, ...CartesianProductElement<W>]
: [];
export function* cartesian<T extends unknown[][]>(
...args: T
): Generator<CartesianProductElement<T>, void, undefined> {
if (args.length === 0) yield [] as any;
else {
const [head, ...rest] = args;
for (const h of head) {
const restIter = cartesian(...rest);
for (const r of restIter) {
yield [h, ...r] as any;
}
}
}
}
cartesian([1, 2], ['a', 'b']) // => [[1,'a'], [1,'b'], [2,'a'], [2,'b']]