I have a function with the following signature:
test<T>(...args: T[]): T
if I call the function like this:
const a = test(1, 2, 3)
everything works as expected (and a
is of type number
), but if I call it that way:
const a = test(1, 2, "asd")
I get any error:
[EDIT] The 2 is red underlined: Argument of type '2' is not assignable to parameter of type '1'.
, witch might be a bit misleading because the arguments are constants, but if I call it that way:
let arg1 = 1;
let arg2 = "asd";
const a = test(arg1, arg2);
I get the error: Argument of type 'string' is not assignable to parameter of type 'number'.
How can I make it so that the function would (in the second case) has a return type of number | string
without explicitly specifying it in the generic parameter.