I've encountered an issue with the function I defined:
function someFunc<T extends any[]>(callback: (...args: T) => void, params: T) {}
Unexpected behavior occurs when calling it in TypeScript:
// this works
// hovering over a, b, and c reveals that they're numbers
someFunc((a, b, c) => {}, [1, 2, 3]);
// this doesn't work
// hovering over a, b, and c reveals that they're of type `number | string`
someFunc((a, b, c) => {}, [1, '2', 3]);
It seems like the issue lies in how TypeScript interprets the second argument of someFunc
as an array rather than a tuple, resulting in types like number[]
or (number | string)[]
.
How can I enforce each parameter's type to match appropriately within the parameters tuple?