4

I get the following error in Typescript:

Argument of type 'number[]' is not assignable to parameter of type 'number'

I get the max value of array without the below error:

analysis_horizon_array_test: number[] = [];

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

How do I get the max value of an array without throwing an error? It does work though.

halfer
  • 19,824
  • 17
  • 99
  • 186
Tampa
  • 75,446
  • 119
  • 278
  • 425
  • 3
    Possible duplicate with http://stackoverflow.com/questions/1669190/javascript-min-max-array-values – Petr Adam Dec 08 '16 at 07:54
  • The only reason this "works" is because the array has one element. `Math.max([2, 6])` is `NaN`. – Ryan Cavanaugh Dec 08 '16 at 08:20
  • Tampa, it looks like you have a good answer below. Would you accept it, please? It is customary to respond in some fashion to helpful people here. – halfer Jan 09 '17 at 08:51

1 Answers1

11

Math.max takes individual numbers, not an array. You can use spread syntax if you're compiling to a supported target, otherwise you have to use apply:

console.log(Math.max(...this.analysis_horizon_array_test));
Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139