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/isFinite
etc
When these methods return true, the value should be asserted to be non-null Number type.
This sounds like a good idea. One minor correction, these are not methods of Number.prototype but rather of the Number function.
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?
function isInteger(value: number): value is number & .....? { // <--- replace .....? with something meaningful
}
you would only use number. the argument should allow non-numeric types as well.
declare function isInteger(value: any): value is number ;
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));
}
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.
Most helpful comment
i really like it! (suggestion! accepting prs!)
so what exactly is an integer number as far as the TypeScript types go?