Understanding that [number, number] | [number]
is an extension of [number, ...number[]]
is logical, but I'm curious if there's a method to enforce the length of tuples based on the initial parameter so that the second tuple must match that same length.
function addVectors<T extends [number, ...number[]]>(v1: T, v2: T) {
// not yet implemented
}
// T becomes [number].
addVectors([1], [2]);
// T becomes [number, number] | [number].
addVectors([1, 3], [3]);
I am looking to avoid this scenario:
addVectors([1, 3], [3]);
Is it feasible to accomplish this in TypeScript?