Here is a function to extract items from an array based on a checker function:
function extractItemsFromArray(array: any[], isItemToBeRemoved: (item: any) => boolean) {
let removedItems = [];
let i = array.length;
while(i--)
if(isItemToBeRemoved(array[i]))
removedItems.push(array.splice(i, 1));
return removedItems;
}
The function accepts an array of any items and a checker function that determines whether to remove an item. However, there is a potential issue if a function with the signature (item: string) => boolean
is passed as isItemToBeRemoved
, and a number[]
is passed as array
. The function definition does not specify that these two any
types should be the same. Is there a way to ensure that both instances of any
are the same type in the function definition?