total-typescript / ts-reset

A 'CSS reset' for TypeScript, improving types for common JavaScript API's
https://www.totaltypescript.com/ts-reset
MIT License
7.74k stars 117 forks source link

ts reset for `.filter` is not working in all cases #197

Closed xiduzo closed 2 months ago

xiduzo commented 2 months ago

I am not sure if any of this would even be possible.

Given the following array

const myArray = [1, undefined, 10, undefined, undefined, 0]
          // ^? (number | undefined)[]

I can do the following filter using Boolean

const myFilteredArray = myArray.filter(Boolean)
          // ^? number[]

But when filtering using any other method it does not give back the correct type

const myFilteredArray = myArray.filter(Number.isNaN)
          // ^? (number | undefined)[]

// Below might be impossible, but would be sick if ts can do this
function myCustomFilter(input?: number) {
   return input !== undefined
}

const myFilteredArray = myArray.filter(myCustomFilter)
          // ^? (number | undefined)[]
mattpocock commented 2 months ago

This will be fixed upstream in TS 5.5