Skip to content

When using Number.is*, the parameter's type is not inferred as number #44665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
til-schneider opened this issue Jun 19, 2021 · 4 comments
Closed

Comments

@til-schneider
Copy link

lib Update Request

Configuration Check

My compilation target is ES2015 and my lib is the default.

Missing / Incorrect Definition

Number.isFinite, Number.isInteger, Number.isNaN and Number.isSafeInteger

Sample Code

function foo(value: number | null | undefined) {
    if (Number.isFinite(value)) {
        bar(value)   // value should be inferred as `number` here
    }
}

function bar (value: number) {
    ...
}

Documentation Link

Number.isFinite

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isFinite

This means only values of the type number and are finite return true.

Number.isInteger

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger

If the target value is an integer, return true, otherwise return false.

Number.isNaN

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN

This also means that only values of the type number, that are also NaN, return true.

Number.isSafeInteger

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isSafeInteger

A Boolean indicating whether or not the given value is a number that is a safe integer.

@til-schneider
Copy link
Author

I created a PR that fixes the definition: #44664

@MartinJohns
Copy link
Contributor

This is intentional and there are several existing issues regarding this. Type guards imply that the function matches for all values of the type, but that's not the case for these functions.

@MartinJohns
Copy link
Contributor

See #39090 (comment).

@til-schneider
Copy link
Author

Unfortunately it doesn't work, see comments above or #44664.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants