Javascript TypeScript isNan 只接受一个数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42120046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeScript isNan only accepts a number
提问by Alon
I work with WebStorm 2016.2.2, TypeScript 2.1, Node.js.
我使用 WebStorm 2016.2.2、TypeScript 2.1、Node.js。
For some reason, isNanis declared as a function that only accepts a number:
出于某种原因,isNan被声明为一个只接受数字的函数:
declare function isNaN(number: number): boolean;
I tried to change it to any, but it looks like it doesn't influence on the TSC. I still get the same error:
我试图将其更改为任何,但它似乎不会影响 TSC。我仍然收到相同的错误:
Argument of type 'string' is not assignable to parameter of type 'number'
“字符串”类型的参数不能分配给“数字”类型的参数
My code (simplified):
我的代码(简化):
isNan("10");
How can I solve/workaround it?
我该如何解决/解决它?
Edit:
编辑:
Notice that according to specification, isNan's parameter can be any type: Number.isNan()
请注意,根据规范,isNan 的参数可以是任何类型:Number.isNan()
Also: My code was simplified. I actually receive a parameter that may be either a string or a number, and if it's a string it may be either a stringy number that I would like to convert to number ("10") or a simple string ("Hello world").
另外:我的代码被简化了。我实际上收到一个参数,它可能是一个字符串或一个数字,如果它是一个字符串,它可能是一个我想转换为数字的字符串数字(“10”)或一个简单的字符串(“Hello world”) .
I didn't want to make this question long by including my entire code, but because it caused confusion, this is my real code:
我不想通过包含我的整个代码来延长这个问题,但因为它引起了混乱,这是我的真实代码:
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;
}
回答by Nitzan Tomer
I advise you to implement your code differently.
The reasons:
我建议你以不同的方式实现你的代码。
原因:
- It might be short, but it's not easy to understand what's going on
- Using
isNaNisn't the best option here:isNaN("")returnsfalseas well
- 它可能很短,但并不容易理解发生了什么
- 使用
isNaN是不是最好的选择在这里:isNaN("")回报false以及
You better try to convert the value into a number and check if that's NaNor not (as @smnbbrv wrote):
您最好尝试将值转换为数字并检查它是否正确NaN(如@smnbbrv 所写):
if (typeof expectedValue === "string" && !Number.isNaN(Number(expectedValue))) {
expectedValue = Number(expectedValue);
}
Edit
编辑
You can pass your value as any:
您可以将您的值传递为any:
isNaN(ctualValue as any)
To bypass the compiler check.
绕过编译器检查。
回答by smnbbrv
You should not solve it because this is how JavaScript works.
你不应该解决它,因为这就是 JavaScript 的工作方式。
Just cast the input to number first
只需先将输入转换为数字
Number("10") // 10
Number("abc") // NaN
and then check the result with the isNan function:
然后用 isNan 函数检查结果:
isNaN(Number("abc"))
回答by laser
You can use global scope function isFinite()... do not use Number.isFinite()
您可以使用全局作用域函数isFinite()...不要使用Number.isFinite()
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isFinite
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isFinite
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Number/isFinite
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Number/isFinite
回答by Tamas Hegedus
First of all, only values of type numbercan be NaN. So if the static context tells you your value is of type stringfor 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 NaNis a specific value specified in the IEEE standard for float numbers. But still, in javascript, isNaN("foo")will be true, as the function will coerect the string to a numberfirst, and that coerection results in a NaN. Typescript tries to take advantage of types here, it tries to prevent you from using isNaNwhere you should not.
首先,只有 type 的值number可以是NaN。因此,如果静态上下文告诉您您的值是类型string,例如,您可以确定它不是NaN. 如果您有一个带类型的值string|number(顺便说一下,应该避免使用),您仍然可以决定如何处理它。严格地说,该字符串值"foo"不是NaN,因为NaN在浮点数的IEEE标准规定的特定值。但是,在 javascript 中,仍然isNaN("foo")是正确的,因为该函数首先将字符串强制转换为数字,并且该强制转换结果为NaN. Typescript 尝试在这里利用类型,它试图阻止您使用isNaN不应该使用的地方。
回答by Ellaji
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.
您可以通过在 isNaN 中使用 parseInt 来解决此问题。如果 parseInt 返回 NaN,检查 isNaN 仍然有效。您的 Typescript 错误将得到解决。
if (typeof actualValue === "string" && !isNaN(parseInt(actualValue, 10))) {
actualValue = +actualValue;
}

