Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 810
Tampa Avatar asked Sep 01 '25 01:09

Tampa


1 Answers

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));

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!