The isNumeric function is implemented as a typeguard that returns true for value is number.
The problem is that the function returns true also if the value is a string, that can be parsed to a number.
The typescript type number means strictly a number, not a parseable string.
RxJS version: 6.1.0
Code to reproduce:
const a = '10'
isNumeric(a) === true
typeof a === 'string'
if (isNumeric(a)) {
const b: number = a
}
Expected behavior:
ts compiler should complain, because a ('10') is not assignable to number
Actual behavior:
ts compiler accepts the code, because isNumeric makes an incorrect type guarantee.
Additional information:
This is a bug in the typings, however, isNumeric is internal to the library and not meant for public consumption, really.
@benlesh I am trying to have a look at this.
is this as simple as putting a check on like typeof value !== 'string' ?
@dkosasih No, doing so would be a breaking change to the function's behaviour, as it is currently written to return true for numeric strings. If it does need to be fixed, the fix would be to remove the type predicate so that is just a simple function and not a user-defined type guard. Or to change the type predicate to use number | string.
@cartant Thanks for the insights.
That is even simpler. I'll do a PR for that. Is that OK?
I don't have a problem with it being fixed, as it's incorrectly typed. And I'd favour just changing the type within the type predicate and leaving it as a user-defined type guard. So if you want to submit a PR, sure.
But, as Ben has stated, it's an internal function, so if you are changing it to address an issue in your code base, you should stop importing the function.
No I didn't. Just try to be useful to the community and try to pick something simple to start with
@dkosasih thanks for fixing this :+1:
Most helpful comment
No I didn't. Just try to be useful to the community and try to pick something simple to start with