-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Number methods should support type guards #11670
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
Comments
This sounds like a good idea. One minor correction, these are not methods of |
PRs welcomed. You can find more information about contributing lib.d.ts fixes at https://github.com/Microsoft/TypeScript/blob/master/CONTRIBUTING.md#contributing-libdts-fixes. |
i really like it! (suggestion! accepting prs!) so what exactly is an integer number as far as the TypeScript types go?
|
you would only use
|
oh, nevermind then |
@Aleksey-Bykov still a good question. Maybe in the future TypeScript's type system will be so advanced that it will be able to infer discrete vs continuous types O.O |
This turns out to be a bad idea because type guards need to return true for all of the values in the domain of their guarded type, not just some of them. In other words, with the proposed definition: let x: number | string = ...;
if (typeof x === 'string') {
} else if (isInteger(x)) {
// x: number
} else {
// We assume 'x: never' in this block!
// error
console.log(x.toFixed(2));
} |
…microsoft#11722)" This reverts commit 4fbbbed.
…microsoft#11722)" This reverts commit 4fbbbed.
I see. That is definitely a problem. This really did seem like a good idea but I guess I didn't really think it through. |
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isSafeInteger
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isFinite
etc
When these methods return
true
, the value should be asserted to be non-null Number type.The text was updated successfully, but these errors were encountered: