Last updated: Mar 7, 2024
Reading time·3 min
arrow-parens
ESLint ruleThe ESLint warning "Expected parentheses around arrow function argument. (arrow-parens)" is shown when you don't wrap an arrow function argument in parentheses.
To resolve the issue, wrap the argument in parentheses or update your ESLint config.
Here is an example of when the warning is shown.
// ⛔️ Expected parentheses around arrow function argument. (arrow-parens) const increment = num => { return num + 1; };
When an arrow function has a single parameter, you aren't syntactically required to wrap the parameter in parentheses.
However, if you want to enforce this, leave the rule as is and wrap the parameter.
// ✅ No ESLint warnings const increment = (num) => { return num + 1; };
If you write your code in TypeScript, you have to wrap the parameter in parentheses to specify the type.
const increment = (num: number) => { return num + 1; };
If your function takes two or more parameters, they also have to be wrapped in parentheses.
const sum = (a, b) => { return a + b; };
However, you can also configure the rule to discourage the use of parentheses when they are not required.
For example, syntactically, the following two arrow functions are equivalent.
// ✅ Functions are equivalent const funcA = x => x + 1 const funcB = (x) => x + 1
However, if your function takes two or more parameters, you are required to wrap them in parentheses.
// ✅ Works const funcA = (a, b) => { return a + b; }; // ⛔️ Error (multiple arguments not wrapped in parentheses) const funcA = a, b => { return a + b; };
By default, the arrow-parens
rule is set to always
require parentheses
around arguments.
However, you can set the value of the rule to as-needed
to enforce that
parentheses should only be used when needed.
Open your .eslintrc.js
file and make the following change.
module.exports = { rules: { 'arrow-parens': ["error", "as-needed"] }, };
The following code causes no warnings when the arrow-parens
rule is set to
as-needed
.
// ✅ No warnings const greet = name => { return `hello ${name}`; };
The as-needed
rule enforces that no parentheses should be used where they can
be omitted.
If you use a .eslintrc
or .eslintrc.json
file, make sure to double-quote the
properties and values.
{ "rules": { "arrow-parens": ["error", "as-needed"] } }
arrow-parens
ESLint ruleIf you simply want to disable the arrow-parens
ESLint rule, edit your
.eslintrc.js
file as follows.
module.exports = { rules: { 'arrow-parens': 'off' }, };
If you use a .eslintrc
or .eslintrc.json
file, make sure to double-quote the
properties and values.
{ "rules": { "arrow-parens": "off" } }
After you disable the rule, you won't get any ESLint warnings or errors as it relates to wrapping function parameters in parentheses.
You can learn more about the related topics by checking out the following tutorials: