reduce is called without an initial value and throws on an empty array

tx0/ts:reduce-without-initialstrictwarningsmell

What it finds

totals.reduce((sum, t) => sum + t): without an initial value, reduce throws a TypeError on an empty array, and the first element becomes the accumulator, so a callback that expects a different type for it misreads the first step.

Only calls with a function literal as the single argument are reported. Code that knows the array is never empty can leave the initial value out, which is why the rule is strict.

From the rule's source, reduce_without_initial.rs.

Example from the rule's tests
function total(values: number[]) {  const sum = values.reduce((acc, v) => acc + v);tx0 reports: reduce without an initial value throws on an empty array  return sum + values.reduce((acc, v) => acc + v, 0);}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/ts:reduce-without-initial" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/ts:reduce-without-initial] reason="..."

tx0 explain tx0/ts:reduce-without-initial shows the same in the terminal. Found a false positive? Report it.