0

I am new to TypeScript and I am trying following

 const storedNumber = [1,2,3,4,5];
 Math.max(storedNumber)

but I kept typescript error saying

argument of type 'any ' is not assignable to parameter of type number

localhost
  • 822
  • 2
  • 20
  • 51
  • Read the documentation on [`Math.max([value1[, value2[, ...]]])`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max) – Andreas Apr 09 '18 at 15:09
  • 1
    `Math.max` doesn't accept an array. It accepts discrete values. If you need to get the max of an array, use `var result = Math.max.apply(Math, theArray)` or on modern JavaScript environments `var result = Math.max(...theArray);`. – T.J. Crowder Apr 09 '18 at 15:12

1 Answers1

2

Math.max requires numbers as arguments like Math.max(1, 3, 2) not an array. You can spread the array.

const storedNumber = [1,2,3,4,5];

console.log(  Math.max(...storedNumber) );

Doc: Math.max(), Spread

Eddie
  • 26,593
  • 6
  • 36
  • 58