I running Angular 11 and I have the following expression with checks to avoid calling reduce on a undefined value:
(this.budgetConfiguration?.consumption ?? []).reduce((accumulatorValue, currentValue) => (accumulatorValue || 0) + (currentValue || 0) , 0);
which throws the following error:
core.js:5980 ERROR TypeError: (intermediate value)(intermediate value)(intermediate value).reduce is not a function
But if I execute that expression just as it is(you can try, just copy and paste) in chrome console returns a 0 value that is what I expect from my piece of code.
It's not a complex sentence, I just using elvis operator to change undefined for an empty array when is required.
So, what i'm doing wrong? Why Typescript doesn't like this?
Thanks