Reduce函数返回赋值触发ESLint错误(no-return-assign)的解决方法
Let’s break down how to resolve this issue cleanly. The root problem here is that your arrow function in reduce is returning an assignment (bal += parseInt(coin.balance)) directly, which triggers ESLint’s no-return-assign rule. Even wrapping the assignment in parentheses might not work if your ESLint configuration is set to strict mode, and it’s not the most idiomatic approach anyway.
Here are two solid solutions:
Option 1: Return the sum directly (simplest fix)
Instead of using an assignment, just return the cumulative balance plus the parsed coin value. This eliminates the assignment entirely and plays nicely with ESLint’s rules:
const calculateBalance = (coins) => { console.log('coins', coins); return coins.reduce((bal, coin) => bal + parseInt(coin.balance), 0); };
Option 2: Use a block arrow function with explicit return
If you prefer to keep the assignment for readability, switch to a block-style arrow function. Do the assignment first, then explicitly return the updated balance—this avoids returning an assignment directly:
const calculateBalance = (coins) => { console.log('coins', coins); return coins.reduce((bal, coin) => { bal += parseInt(coin.balance); return bal; }, 0); };
Why your initial parentheses fix might have failed
The no-return-assign rule has two main configurations:
except-parens(default): Allows assignments wrapped in parenthesesalways: Blocks any assignment in a return statement, even when wrapped in parentheses
If your ESLint setup uses the stricter always setting, wrapping the assignment in parentheses won’t resolve the error. Both options above avoid this problem entirely, making your code compliant no matter how the rule is configured.
内容的提问来源于stack exchange,提问作者Leon Gaban

