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:

  1. Get a number as a string.
  2. Convert it to a number: var num = Number(myStrNum);
  3. 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:

  1. Get a number as a string.
  2. Convert it to a number: var num = Number(myStrNum);
  3. 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
  • "I cannot trust that a number is not not a number": can you give a concrete example? – trincot Commented Feb 23 at 12:35
  • 2 But NaN is a number. It’s a category error to imagine otherwise. It’s a special floating point number indicative of an error. Had it been named NumErr 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:43
  • 1 This question is similar to: TypeScript isNaN only accepts a number. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – Matthieu Riegler Commented Feb 23 at 12:59
  • I remain confused by this question so I really hope to see a clarifying edit. The title makes it sound like just one of the many questions about why TS doesn't allow, say isNaN(""), and the answer is it does confusing things. But the main question makes it sound like you wonder why TS accepts that NaN is a number, 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
Add a comment  | 

2 Answers 2

Reset to default 0

You'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