相关文章推荐
大鼻子的大熊猫  ·  .net core vs .net ...·  1 年前    · 
谈吐大方的爆米花  ·  在python ...·  1 年前    · 
帅气的伏特加  ·  git remote prune ...·  1 年前    · 

typescript parseint argument of type 'number' is not assignable to parameter of type 'string'

在 TypeScript 中, parseInt() 是一个内置函数,用于将字符串转换为整数。它接受两个参数:

  • 一个字符串,表示要转换的值。
  • 一个数字,表示要转换的字符串的基数(即进制)。
  • 如果你在调用 parseInt() 时传入了一个数字类型的值,那么 TypeScript 就会报错,提示「argument of type 'number' is not assignable to parameter of type 'string'」,意思是参数类型为 'number' 的值不能赋给参数类型为 'string' 的参数。

    举个例子,如果你想将数字 100 转换为十进制,你可以这样写:

    const num: number = 100;
    const result: number = parseInt(num.toString(), 10);
    

    或者,你也可以直接将数字 100 转换为字符串,然后传给 parseInt() 函数:

    const result: number = parseInt('100', 10);
    

    希望这些信息能帮到你。

  •