Skip to content

Number typeguards #41569

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
jods4 opened this issue Nov 17, 2020 · 2 comments
Closed

Number typeguards #41569

jods4 opened this issue Nov 17, 2020 · 2 comments

Comments

@jods4
Copy link

jods4 commented Nov 17, 2020

TypeScript Version: 4.1.1-rc

Search Terms: Number typeguard

Code

let x = 42 as number | null;
Number.isFinite(x) && x.toString()

Expected behavior: No error nor warnings.
The following built-in Number functions should be defined as typeguards as they can only return true when their argument is a number:
isFinite, isInteger, isNaN, isSafeInteger.
They currently typed as simple boolean and hence do not modify TS flow analysis.

Actual behavior: TS2531, objet is possibly null

Playground Link: https://www.typescriptlang.org/play?ts=4.1.0-beta#code/DYUwLgBAHhC8EBYBMECGBnCA7ArgWwCMQAnCAH2x2GAG4AoAOXyOIDoBLdAMXa3bBAAKKAEoIAMnHRWYAPYBlMMV4BzQSJpA

Related Issues:

@MartinJohns
Copy link
Contributor

Duplicate of #39090. Search terms used: isFinite

@jods4
Copy link
Author

jods4 commented Nov 17, 2020

Indeed.

@jods4 jods4 closed this as completed Nov 17, 2020
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