在 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);
希望这些信息能帮到你。