Presented here is the combined type of two signatures for the filter function and the function itself.
type Filter = {
(arr: string[], f: (item: string) => boolean): string[]
(arr: number[], f: (item: number) => boolean): number[]
}
let filter: Filter = (arr, func) => {
let result = []
for (let i in arr) {
let item = arr[i]
if (func(item)) {
result.push(item)
}
}
return result
}
The compiler recognizes this as a function of the union type:
https://i.sstatic.net/x5zKX.png
However, within the function, it fails to recognize the argument item
and labels it as never