I have a recursive function with 2 overloads :
export function select(
array: Float32Array,
first: number,
nth: number,
last: number,
comp: (a: number, b: number) => boolean,
): void;
export function select<T>(
array: T[],
first: number,
nth: number,
last: number,
comp: (a: T, b: T) => boolean,
): void;
export function select<T>(
array: Float32Array | T[],
first: number,
nth: number,
last: number,
comp: (a: number | T, b: number | T) => boolean,
): void {
// Implementation of Floyd-Rivest algorithm
// Some code
select(array, newFirst, nth, newLast, comp);
// Some code
}
Typescript complains about variable array when calling select function recursively in the implementation :
The argument of type 'Float32Array | T[]' is not assignable to the parameter of type '(number | T)[]'.
First, I don't really understand why typescript try to compare the type of argument array with a type (number | T)[] which does not exist in the different signatures. Does it try to compare the type of array with arguments type of comp function?
Of course I can replace the type of the argument array by any in the implementation signature, it works, but I would like to know if there is a better way to handle this case.
T[]toArrayLike<T>? Since you also allow aFloat32Arrayyou will not be using any of the Array prototype function. If you also use it in a for-of loop or something you may addArrayLike<T> & Iterable<T>.