Let's take a look at a function with the following signature:
function removeNumbersOrStringsElementsFromArray(
targetArray: Array<number | string>,
targetElementOrMultipleOfThem: number | string | Array<number | string>
): {
updatedArray: Array<number | string>;
removedElementsIndexes: Array<number>;
} {
// implementation
}
Even if all the elements in targetArray
are either all strings or all numbers,
const result: Array<string> = removeNumbersOrStringsElementsFromArray([ 1, 2, 3, 4 ], [ 1, 2 ]).updatedArray;
this will result in an error:
“Type (string|number) is not assignable to type string[]”
One solution could be to change the signature from Array<number | string>
to
Array<number> | Array<string>
, but that would require changing the implementation of removeNumbersOrStringsElementsFromArray
. However, from a JavaScript perspective, it could work. Are there any other solutions?