Argument of 'string | number' type string is not assignable to type never

function remove(arr: number | string, valueToRemove: string | number) {
return arr.indexOf(valueToRemove);
};

const numbers = [1, 2, 3];
const names = [“John”, “Alice”, “Ellen”];

const newNumbers = remove(numbers, 2);
const newNames = remove(names, “Ellen”);

console.log(newNumbers);
console.log(newNames);

/*
Expected output:

  [1, 3]
  [John, Alice]

*/

If you have union types with arrays, you need to group them. Try
arr: (number | string)[], valueToRemove: (string | number)[]
instead.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.