function isDefined<T>(argument: T | undefined): argument is T {
return typeof argument !== 'undefined'
}
const items = [1, 2, undefined]
items.filter(isDefined).map((number) => {
// number is now type guarded and undefined is eliminated
console.log(number)
})
items.filter((num) => isDefined(num)).map((number) => {
// however, this way it does not work, and "number" can still be undefined
console.log(number)
})
I am not sure why TypeScript is not inferring the results in the second way.
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too