【问题标题】:Difference between arg: number as function parameters, and Number(arg)arg: number 作为函数参数和 Number(arg) 之间的区别
【发布时间】:2022-06-14 21:58:18
【问题描述】:

在学习 NestJS 教程后,我遇到了一些引起我注意的事情(来自 service.ts 文件):

public async putCarById(id: number, property_name: string, property_value: string)// : Promise<any>
    {
        console.log(id);        // gives provided number, for example, 1
        const carId = Number(id); 
        console.log(typeof (id)); // will output "string" - why ?
        console.log(typeof (carId)); // will output "number" -- which I understand

(...)

我找不到为什么必须进行“强制转换”,而 id 应该作为数字接收。

【问题讨论】:

  • 大概类型是谎言; 在运行时它得到一个字符串,所以参数应该反映这一点。

标签: java node.js typescript parameters nestjs


猜你喜欢
  • 1970-01-01
  • 2017-10-17
  • 1970-01-01
  • 2016-03-23
  • 1970-01-01
  • 2016-12-23
  • 2020-12-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多