Rxjs: Typing of isNumeric function is incorrect

Created on 15 May 2018  路  7Comments  路  Source: ReactiveX/rxjs

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.

https://github.com/ReactiveX/rxjs/blob/92dd3d135d3fcf7fa98526a2ba0493754407a94a/src/internal/util/isNumeric.ts

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:

Help Wanted TS types bug

Most helpful comment

No I didn't. Just try to be useful to the community and try to pick something simple to start with

All 7 comments

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:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dooreelko picture dooreelko  路  3Comments

marcusradell picture marcusradell  路  4Comments

benlesh picture benlesh  路  3Comments

matthewwithanm picture matthewwithanm  路  4Comments

benlesh picture benlesh  路  3Comments