代码之家  ›  专栏  ›  技术社区  ›  manidos

isNaN(+str!)的语法是什么

  •  1
  • manidos  · 技术社区  · 6 年前

    here

    export function convertToUnit (str: string | number | null | undefined, unit = 'px'): string | undefined {
      if (str == null || str === '') {
        return undefined
      } else if (isNaN(+str!)) { // **** THIS LINE ****
        return String(str)
      } else {
        return `${Number(str)}${unit}`
      }
    }
    
    1 回复  |  直到 6 年前
        1
  •  5
  •   CertainPerformance    6 年前

    表情 +str! 合并(JavaScript运算符) unary plus 使用(TypeScript独占运算符) non-null assertion operator

    这个 ! 声称 str 在 + 完成了。但是,由于 str == null str 是 null 也没有 undefined 在这一点上:断言是不必要的。

    +str
    

    哪个演员 str 到一个数字。然后将该数值传递给 isNaN ,它将返回 true 如果值为 NaN false .

        2
  •  2
  •   Andy Vaal    6 年前

    + ,或一元加号运算符,用于转换对象( str this answer .

    这个 ! str 不为null,以禁止编译器以其他方式发出的任何警告。你可以读更多 here .

    如果 str 不是一个数字,那么 操作将返回“非数字”,或 NaN isNaN 如果 楠 str 是一个有效的数字。

    推荐文章