https://type-level-typescript.com/arrays-and-tuples#mixing-arrays-and-tuples
이런게 되는건 첨보네.. 타입을 따로 선언해서 사용해야 한다. 함수 선언하면서 가변길이 매개변수 뒤에 일반 매개변수가 올 수는 없음.
Leading Rest Elements
Now that you have seen how similar tuples and function arguments look, you may be thinking that we can always swap one for the other.
This is true in most cases, but here is something you wouldn’t be able to do with regular function arguments — Leading Rest Elements. It’s the ability to use the ...
syntax before other elements in a tuple type.
As an example, let’s type the zipWith function from lodash. zipWith(...arrays, zipper)
takes several arrays, a “zipper” function, and zips them into a single array by calling the zipper function with all values for each index.
Here is a possible way of typing zipWith
:
type ZipWithArgs<I, O> = [
...arrays: I[][], // <- Leading rest element!
zipper: (...values: I[]) => O
];
declare function zipWith<I, O>(...args: ZipWithArgs<I, O>): O[];
// ^ The `declare` keyword lets us define a type
// without specifying an implementation
const res = zipWith(
[0, 1, 2, 3, 4],
[1930, 1987, 1964, 2013, 1993],
[149, 170, 186, 155, 180],
(index, year, height) => {
// index, year, and height are inferred as
// numbers!
return [index, year, height];
}
)
You couldn’t type this function with regular parameter types because the JavaScript syntax doesn’t support leading rest elements:
declare function zipWith<I, O>(
...arrays: I[][], /* ~~~
^ ❌ A rest parameter must be last in a parameter list */
fn: (...values: I[]) => O
): O[];
The good news is that they are supported at the type level!