TypeScript isNaN 只接受一个数字

我使用 WebStorm 2016.2.2,TypeScript 2.1,Node.js。

由于某种原因,isNaN被声明为只接受一个数字的函数:

declare function isNaN(number: number): boolean;

我试着改成任何一种,但看起来对 TSC 没有影响。我仍然得到相同的错误:

类型为“ string”的参数不可赋值给类型为 号码

我的代码(简体) :

isNaN("10");

我如何解决/解决这个问题?


编辑:

注意,根据规范,isNaN 的参数可以是任何类型: Number.isNaN ()

另外: 我的代码被简化了。实际上,我接收到的参数可能是一个字符串或一个数字,如果它是一个字符串,它可能是一个字符串数字,我想转换为数字(“10”)或一个简单的字符串(“ Hello world”)。

我不想因为包含了我的整个代码而使这个问题变得冗长,但是因为它引起了混乱,所以这才是我真正的代码:

            if (typeof expectedValue === "string" && !isNaN(expectedValue)) {
expectedValue = +expectedValue;
}


if (typeof actualValue === "string" && !isNaN(ctualValue)) {
actualValue = +actualValue;
}


switch (this.operator) {
case Operator.equal:
return actualValue == expectedValue;
case Operator.notEqual:
return actualValue === undefined || actualValue != expectedValue;
case Operator.greaterThan:
return actualValue > expectedValue;
case Operator.littleThan:
return actualValue < expectedValue;
case Operator.greaterOrEqual:
return actualValue >= expectedValue;
case Operator.littleOrEqual:
return actualValue <= expectedValue;
}
52656 次浏览

You should not solve it because this is how TypeScript works.

Just cast the input to number first

Number("10") // 10
Number("abc") // NaN

and then check the result with the isNan function:

isNaN(Number("abc"))

First of all, only values of type number can be NaN. So if the static context tells you your value is of type string for example, you can be sure that it is not a NaN. If you have a value with type string|number (which should be avoided btw) you can still decide how you handle this. Strictly speaking, the string value "foo" is not NaN, as NaN is a specific value specified in the IEEE standard for float numbers. But still, in javascript, isNaN("foo") will be true, as the function NaN1 first, and that coerection results in a NaN. Typescript tries to take advantage of types here, it tries to prevent you from using NaN0 where you should not.

I advise you to implement your code differently.
The reasons:

  1. It might be short, but it's not easy to understand what's going on
  2. Using isNaN isn't the best option here: isNaN("") returns false as well

You better try to convert the value into a number and check if that's NaN or not (as @smnbbrv wrote):

if (typeof expectedValue === "string" && !Number.isNaN(Number(expectedValue))) {
expectedValue = Number(expectedValue);
}

Edit

You can pass your value as any:

isNaN(ctualValue as any)

To bypass the compiler check.

You can solve this by using parseInt inside your isNaN. The check isNaN will still work if the parseInt returns NaN. And your Typescript error will be solved.

if (typeof actualValue === "string" && !isNaN(parseInt(actualValue, 10))) {
actualValue = +actualValue;
}

As ironically only numbers can be NaN, you need to transform strings into numbers first.

A very simple way to do this is the unary plus operator.

So you can do a simple isNaN(+"10").

Keep in mind that thing like +"", +" " and +"\n" are 0!

In the accepted answer, !Number.isNaN(Number(expectedValue)) still returns true for empty string ('') and whitespace strings (' '). And converting these to number will result in 0.

I'm not a JavaScript developer, and – especially coming from .Net – it looks insane to me as well, but this is what I've done that seems to work:

private static isNumber(value: any): boolean {
return (typeof value === 'number' && !isNaN(value))
|| ((typeof value === 'string') && value.trim() != '' && !isNaN(Number(value)))
}

If you know a saner solution, be sure to edit this!

console.log(isNumber([]));      // false
console.log(isNumber({}));      // false
console.log(isNumber(""));      // false
console.log(isNumber("   "));   // false
console.log(isNumber(" 1  "));  // true <= note
console.log(isNumber(" 1  2")); // false
console.log(isNumber("1"));     // true
console.log(isNumber(1));       // true

Found this answer from Google, and reached my answer from the various answers and comments here; my answer is to use:

isNaN(Number(string))

This will correctly tell me if the string is a number.

I was previously using parseInt(), but this fails if the string is something like 123abc as parseInt just discards the letters (useful sometimes, but not here).

Note: I'm happy that Number('') evaluates to zero, but if your not, this isn't the solution!

Passing isNaN(value as unknown as number) satisfied my compiler. In my case, I was using isNaN() to prevent "NaN" from flashing while data loaded. This allowed me to pass a string into isNaN() since the interface expected a string.