【发布时间】:2022-01-05 03:30:28
【问题描述】:
我相信 'strVariable as number' 将 strVariable 转换为数字类型。
但是挣扎了几个小时,我发现它的类型没有改变。
Number(strVariable) 是关键。
'as number' 和 'Number()' 有什么区别?
如果'as number'实际上并没有将类型转换为数字,为什么IDE的类型错误被删除??
const strVariable: string = '';
const numVariable: number = 0;
numVariable = strVariable as unknown as number; // I thought it is converting to number but it wasn't
typeof (strVariable as unknown as number); // string. but type error is gone. How?
typeof (Number(strVariable)) // it do converts to number.
【问题讨论】:
标签: reactjs typescript types numbers