I wrote a small function to perform multi-dimensional combination of array elements. Example:
test('combine', () => {
const result = combine([[[1, 2], [3, 4]], [['a', 'b'], ['c', 'd']]])
expect(result).toStrictEqual([
[[ 1, 2], ["a", "b"]],
[[ 1, 2], ["c", "d"]],
[[ 3, 4], ["a", "b"]],
[[ 3, 4], ["c", "d"]],
]
)
})
The function works as expected but if I don't use // @ts-ignore I get the following TypeScript error with the following code:
export const combine = (arr: Array<unknown>) => R.apply(R.liftN(arr.length, (...args) => args), arr)
TS2769: No overload matches this call.
The last overload gave the following error.
Argument of type 'unknown[]' is not assignable to parameter of type '[]'.
Target allows only 0 element(s) but source may have more.
7 |
> 8 | export const combine = (arr: Array<unknown>) => R.apply(R.liftN(arr.length, (...args) => args), arr)
| ^^^
Your help will be appreciated.
Ramda has the
R.xprodfunction, which does the same thing. This also works out of the box with TS.