The result of forEach, which is always undefined, is stored

tx0/ts:foreach-result-usedrecommendedwarningbug

What it finds

const names = users.forEach((u) => u.name): forEach always returns undefined, so a variable or return value taken from it is undefined. map was meant.

Reported are forEach calls whose result initializes a variable or is assigned. The first argument must be a function literal, as the callback of the array method is; return items.forEach(f), a common way to run the loop and leave a function that returns nothing, is left alone. Libraries have forEach methods that return their collection, which is why the rule is recommended rather than on by default.

From the rule's source, foreach_result_used.rs.

Example from the rule's tests
function names(users: { name: string }[]) {  const all = users.forEach((u) => u.name);tx0 reports: forEach returns undefined, which is stored here  let last;  last = users.forEach((u) => u.name);tx0 reports: forEach returns undefined, which is assigned here  users.forEach((u) => console.log(u.name));  return [all, last];}function done(users: string[]) {  return users.forEach((u) => console.log(u));}
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 recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/ts:foreach-result-used" = "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:foreach-result-used] reason="..."

tx0 explain tx0/ts:foreach-result-used shows the same in the terminal. Found a false positive? Report it.