【问题标题】:Typescript error "Argument of type 'number[]' is not assignable to parameter of type 'number'"打字稿错误“'number []'类型的参数不可分配给'number'类型的参数”
【发布时间】:2017-04-23 09:26:28
【问题描述】:

我在 Typescript 中收到以下错误:

“number[]”类型的参数不能分配给“number”类型的参数

我得到数组的最大值,没有以下错误:

analysis_horizon_array_test: number[] = [];

this.analysis_horizon_array_test.push(1)
console.log(Math.max(this.analysis_horizon_array_test));

如何在不引发错误的情况下获取数组的最大值?但它确实有效。

【问题讨论】:

  • 这个“有效”的唯一原因是数组只有一个元素。 Math.max([2, 6])NaN
  • 坦帕,看起来你在下面有一个很好的答案。请问你能接受吗?习惯上以某种方式回应这里有帮助的人。

标签: angular typescript


【解决方案1】:

Math.max 接受单个数字,而不是数组。如果要编译到受支持的目标,则可以使用扩展语法,否则必须使用 apply:

console.log(Math.max(...this.analysis_horizon_array_test));

【讨论】:

    猜你喜欢
    • 2023-01-16
    • 1970-01-01
    • 2020-06-19
    • 2019-10-23
    • 2020-06-19
    • 1970-01-01
    • 1970-01-01
    • 2018-08-15
    • 2020-04-04
    相关资源
    最近更新 更多