You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
"Number.isFinite" and "Number.isInteger" do not filter out values as a predicate #39090
As far as TS is concerned, it's just like any other function, a black box that returns true or false. There's no special case in the compiler that says using Number.isFinite in particular will remove all the nulls.
edit: I forgot type predicates are a thing, ignore the above brainfart.
Note that the function doesn't work as a type predicate in general either:
letx: number|null=777asany;if(Number.isFinite(x)){x;// still number | null}
The fix would be for these functions to be declared as predicates, e.g.
Ah right, because otherwise !Number.isInteger(x) implies that x is not a number and would be narrowed as such. I forgot that that works both ways when I made the above suggestion.
Reacted by Ryan Cavanaugh, Max Donchenko, Eliaz Bobadilla and Jesse Jackson
Activity
fatcerberus commentedon Jun 16, 2020
As far as TS is concerned, it's just like any other function, a black box that returns true or false. There's no special case in the compiler that says usingNumber.isFinitein particular will remove all the nulls.edit: I forgot type predicates are a thing, ignore the above brainfart.
Note that the function doesn't work as a type predicate in general either:
The fix would be for these functions to be declared as predicates, e.g.
RyanCavanaugh commentedon Jun 16, 2020
Type guards are only correctly type guards if they return
truefor every value of the tested type, not some values of the type.See #15048 for supporting type guards that only identify a subset of their type.
fatcerberus commentedon Jun 16, 2020
Ah right, because otherwise
!Number.isInteger(x)implies thatxis not anumberand would be narrowed as such. I forgot that that works both ways when I made the above suggestion.typescript-bot commentedon Jun 18, 2020
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.