~ views

ESLint - Ignore underscore-prefixed variables for eslint-disable-unused-vars rule


Neovim screenshot showing ESLint unused vars error

Ever encounter this error?

"eslint: '\_myVar' is assigned a value but never used"

Ideally the @typescript-eslint/no-unused-vars rule can be left enabled and specific variables can be ignored when prefixed with an underscore _. You can do exactly this in a few simple steps.

First, update your .eslintrc.json as follows:

{
"eslintConfig": {
"rules": {
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": [
"warn",
{
"argsIgnorePattern": "^_",
"varsIgnorePattern": "^_",
"caughtErrorsIgnorePattern": "^_"
}
]
}
}
}

This allows you to prefix any variable with _ to be ignored by ESLint, including those in destructuring assignments.

After updating the .eslintrc.json as follows, I could then prefix any variable with an underscore to have it be ignored by ESLint:

const [_isZoomVisible, setZoomVisible] = useState(false);

added underscore to variable name.

After updating the eslintConfig, the error is now gone and ESLint doesn’t complain about the unused variables.

Back to top