For the purpose of this explanation, I will completely disregard the use of comparator
. Additionally, I am assuming that you are not concerned with paying attention to the length of each array element. So, for example, calling
sortArraysTogether([["a","b"], [1,2,3]])
will not result in an error even though the second element contains too many entries.
In this scenario, the emphasis lies on preserving the order of elements within the arrays
and the return type. Therefore, the desired output of
sortArraysTogether([["a"],[1],[true]])
should be
[string[], number[], boolean[]]
rather than
(string | number | boolean)[][]
. In essence, a plain array format like
⋯[]
is avoided because it does not maintain order. The output type needs to be made generic so that it can represent a tuple that adapts based on the input type.
Furthermore, since maintaining order is crucial, it makes sense to enforce the requirement of having at least one element present. Hence, invoking sortArraysTogether([])
should trigger an error.
Keeping these requirements in mind, the structure of sortArraysTogether()
would resemble:
declare function sortArraysTogether<T extends [any, ...any[]]>(
arrays: { [I in keyof T]: T[I][] },
): { [I in keyof T]: T[I][] };
In this implementation, the function is designed to be generic in T
, which represents the element type of each element within arrays
. For instance, when calling
sortArraysTogether([[0],["a"],[true]])
,
T
will be inferred as
[number, string, boolean]
. By placing constraints on
T
to be within an open-ended tuple
[any, ...any[]]
, TypeScript gets a hint about the importance of order and enforces the presence of at least one element in the input.
Both the type of arrays
and the output type are defined as {[I in keyof T]: T[I][]}
, representing a mapped tuple type where each element in arrays
and the output mirrors an array of the corresponding element in T
. Therefore, if T
is stated as [A, B, C, D]
, then both the type of arrays
and the return type would be [A[], B[], C[], D[]]
.
To verify the functionality:
const z = sortArraysTogether([[20, 10, 30], ['bob', 'alice', 'charlie'], [1, 3, 10]]);
// ^ const z: [number[], string[], number[]]
sortArraysTogether([]); // error!
// ~~
// Argument of type '[]' is not assignable to parameter of type '[any[], ...any[][]]'.
The outcome appears satisfactory.
Playground link to code