-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
lib.d.ts contains wrong typings for Number.isFinite() #15527
Comments
Duplicate of #4002 |
Yes, I think In this context the right signature for isFinite(arg: any): arg is number; In my opinion, PS: the global Number.isFinite('123'); // false
isFinite('123'); // true |
|
@gcnew: @RyanCavanaugh is right, the typing should be: isFinite(arg: any): boolean; |
not sure i understand why this issue is different from #4002. |
I think the logic should be the reverse. Every value for which Edit: In my opinion a better approach is to model these known corner case constants as special cases. Number should not include them in a strict setting the same way types don't include |
just to be clear, are we talking about the argument type for I am assuming this issue is about the earlier. |
From my perspective, if |
I guess it should should be Number.isFinite(argument : any) : boolean |
@gcnew see #15048 for type guards which don't cover their entire domains Regarding accepting |
The reason that it is desirable is the following: /**
* @param n A finite number (optional)
*/
function doSomething(n?: number) {
if (Number.isFinite(n)) {
// process user input
}
} is less to type and less to execute than
And the ECMA spec says I can simply use the first example but the typescript compiler simply won't compile it I've seen people do all kinds of things like
and usually they'll forget the null check in a couple of cases. FINALLY I can give all my team a guideline that will always work without surprises and it's called Number.isFinite(). It just works. I only need it to compile with strict null checks on. |
You could also write |
e.g. I'd be really surprised if I wrote |
Ok so maybe I constructed the wrong example but I've also seen a lot of this if (!Number.isFinite(userInput)) {
throw new Error("boo");
} That's a very nice catch-all that checks exactly what you want to check |
We also do in this way: if(userNumber typeOf number) {
if(!Number isFinite(userNumber)) {
console.log("Error")
}
}
else {
console.log("Not a number");
} |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
I'm not sure whether this is the repo to be putting this issue on, please let me know if it isn't.
TypeScript Version: 2.2.2
Code
Compiling with strict null checks:
Expected behavior:
No compile error. The argument to isFinite should be any, because the function is used to check the type. According to the spec at https://www.ecma-international.org/ecma-262/6.0/#sec-number.isfinite the type is checked by the function.
Actual behavior:
error TS2345: Argument of type 'undefined' is not assignable to parameter of type 'number'
The text was updated successfully, but these errors were encountered: