在Typescript中,这将显示一个错误,表示isNaN只接受数值

isNaN('9BX46B6A')

返回false,因为parseFloat('9BX46B6A')的值为9

isNaN(parseFloat('9BX46B6A'))

我仍然可以运行的错误显示在Visual Studio,但我想做的正确的方式。

目前,我已经写了这个修改后的函数-

static isNaNModified = (inputStr: string) => {
    var numericRepr = parseFloat(inputStr);
    return isNaN(numericRepr) || numericRepr.toString().length != inputStr.length;
}

当前回答

我的解决方案:

 function isNumeric(val: unknown): val is string | number {
  return (
    !isNaN(Number(Number.parseFloat(String(val)))) &&
    isFinite(Number(val))
  );
}
// true
isNumeric("-10");
isNumeric("0");
isNumeric("0xff");
isNumeric("0xFF");
isNumeric("8e5");
isNumeric("3.1415");
isNumeric("+10");
isNumeric("144");
isNumeric("5");

// false
isNumeric("-0x42");
isNumeric("7.2acdgs");
isNumeric("");
isNumeric({});
isNumeric(NaN);
isNumeric(null);
isNumeric(true);
isNumeric(Infinity);
isNumeric(undefined);
isNumeric([]);
isNumeric("some string");

其他回答

更新

这个方法在rxjs v6中不再可用


我通过使用rxjs库中的isNumeric操作符来解决它(导入rxjs/util/isNumeric

import {isNumeric} from 'rxjs/util/isNumeric';

. . .

var val = "5700";
if (isNumeric(val)){
   alert("it is number !");
}

考虑到你的变量可以是字符串、数字或任何类型——对于Angular/Typescript中的完整数字(非浮点数),你可以使用:

var isFullNumber: boolean = 
    Number.isInteger(Number(yourVariable)) && yourVariable !== null;

正如@tarrbal所指出的那样,我们不能使用just:

Number.isInteger(yourVariable);

要证明这一点,请检查以下三个测试:

let testVariables = [0, 1, "0", "1", "A", {}, -3, 0.1, NaN, null, undefined]; 

let isFullNumber: boolean;
let ix: number = 1;
testVariables.forEach(v => {
isFullNumber = Number.isInteger(v);                         // <---
console.log(ix++, ': ', v, isFullNumber);
})

console.log('--------------');

ix = 1;
testVariables.forEach(v => {
isFullNumber = Number.isInteger(Number(v));                 // <---
console.log(ix++, ': ', v, isFullNumber);
})

console.log('--------------');
ix = 1;
testVariables.forEach(v => {
isFullNumber = Number.isInteger(Number(v)) && v !== null;   // <---
console.log(ix++, ': ', v, isFullNumber);
})

这三个结果:

1   :       0           true
2   :       1           true
3   :       0           false <- would expect true
4   :       1           false <- would expect true
5   :       A           false
6   :       {}          false
7   :       -3          true
8   :       0.1         false
9   :       NaN         false
10  :       null        false
11  :       undefined   false
----------------------------
1   :       0           true
2   :       1           true
3   :       0           true
4   :       1           true
5   :       A           false
6   :       {}          false
7   :       -3          true
8   :       0.1         false
9   :       NaN         false
10  :       null        true <- would expect false
11  :       undefined   false
----------------------------
1   :       0           true
2   :       1           true
3   :       0           true
4   :       1           true
5   :       A           false
6   :       {}          false
7   :       -3          true
8   :       0.1         false
9   :       NaN         false
10  :       null        false
11  :       undefined   false

将字符串转换为数字的方法是使用number,而不是parseFloat。

Number('1234') // 1234
Number('9BX9') // NaN

如果你喜欢速记,你也可以使用一元加号运算符:

+'1234' // 1234
+'9BX9' // NaN

检查NaN时要小心(运算符===和!==不能像NaN那样工作)。使用:

 isNaN(+maybeNumber) // returns true if NaN, otherwise false

我的解决方案:

 function isNumeric(val: unknown): val is string | number {
  return (
    !isNaN(Number(Number.parseFloat(String(val)))) &&
    isFinite(Number(val))
  );
}
// true
isNumeric("-10");
isNumeric("0");
isNumeric("0xff");
isNumeric("0xFF");
isNumeric("8e5");
isNumeric("3.1415");
isNumeric("+10");
isNumeric("144");
isNumeric("5");

// false
isNumeric("-0x42");
isNumeric("7.2acdgs");
isNumeric("");
isNumeric({});
isNumeric(NaN);
isNumeric(null);
isNumeric(true);
isNumeric(Infinity);
isNumeric(undefined);
isNumeric([]);
isNumeric("some string");

大多数情况下,我们想要检查的值是字符串或数字,所以这里是我使用的函数:

const isNumber = (n: string | number): boolean => 
    !isNaN(parseFloat(String(n))) && isFinite(Number(n));

Codesandbox测试。

const willBeTrue = [0.1, '1', '-1', 1, -1, 0, -0, '0', "-0", 2e2, 1e23, 1.1, -0.1, '0.1', '2e2', '1e23', '-0.1', ' 898', '080']

const willBeFalse = ['9BX46B6A', "+''", '', '-0,1', [], '123a', 'a', 'NaN', 1e10000, undefined, null, NaN, Infinity, () => {}]