admin管理员组文章数量:1414628
I know that there are several questions here at SO about isNaN
not taking a string but only a number. My question is about the motivation because I don't understand the decision.
I use TypeScript because I want to get help from a type system and be able to trust it.
To be able to use isNaN
I have to do the following:
- Get a number as a string.
- Convert it to a number:
var num = Number(myStrNum)
; - Check isNaN(num);
My point is that after step #2, the TypeScript type system will treat it as a number, even if it's not a valid number. That also means that I cannot trust that a number is not not a number.
Yes, there are several other places where that can happen. But here, the design of the TypeScript definitions force that behavior, while JavaScript itself allows a string argument.
Why was that design decision made?
I know that there are several questions here at SO about isNaN
not taking a string but only a number. My question is about the motivation because I don't understand the decision.
I use TypeScript because I want to get help from a type system and be able to trust it.
To be able to use isNaN
I have to do the following:
- Get a number as a string.
- Convert it to a number:
var num = Number(myStrNum)
; - Check isNaN(num);
My point is that after step #2, the TypeScript type system will treat it as a number, even if it's not a valid number. That also means that I cannot trust that a number is not not a number.
Yes, there are several other places where that can happen. But here, the design of the TypeScript definitions force that behavior, while JavaScript itself allows a string argument.
Why was that design decision made?
Share Improve this question edited Feb 23 at 13:13 jonrsharpe 122k30 gold badges268 silver badges476 bronze badges asked Feb 23 at 12:20 jgauffinjgauffin 101k45 gold badges245 silver badges377 bronze badges 4 |2 Answers
Reset to default 0You'd wonder by NaN
is number
at runtime and TS makes its best to match JS runtime:
console.log(typeof parseInt('bad number string') === 'number')
In the docs, while describing the global isNaN
and Number.isNaN
it's clearly stated that there are two different motivation for each of those:
Global isNaN
:
The isNaN() function answers the question "is the input functionally equivalent to NaN when used in a number context"
So it has to be a number
to check if it behavies as such.
E.g. x = "2"
you have to convert it into a number if you want to check if it does behave like a number; if it can't even become a number
, surely it will not behave as such.
Number.isNaN()
:
As opposed to the global isNaN() function, the Number.isNaN() method doesn't force-convert the parameter to a number. This makes it safe to pass values that would normally convert to NaN but aren't actually the same value as NaN.
E.g. Try to think a general situation, like when you want to do something to a said value depending on its type. One example could be:
if (!Number.isNaN(x)) {
Console.log(`Done something with a number e.g ${x+2}`);
} else {
Console.log(`Done something with not a number e.g. ${x.toString()}`);
}
In this case you are not only ensuring that x
is a number, you also avoid the "problematic" NaN
value
本文标签: Why does isNaN in TypeScript force a number as an argumentStack Overflow
版权声明:本文标题:Why does isNaN in TypeScript force a number as an argument? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745155226a2645129.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
NaN
is anumber
. It’s a category error to imagine otherwise. It’s a special floating point number indicative of an error. Had it been namedNumErr
instead, would you have asked this question? If so, maybe you should edit to make it look like that version of the question? – jcalz Commented Feb 23 at 12:43isNaN("")
, and the answer is it does confusing things. But the main question makes it sound like you wonder why TS accepts thatNaN
is anumber
, which is the category error I mentioned in my previous comment. Please edit to clarify, especially if you are attracting answers to a q you didn't ask. – jcalz Commented Feb 23 at 14:22