Diagnosing ESLint no‑unused‑vars False Positives in JavaScript and TypeScript
Learn why ESLint's no-unused-vars rule can falsely flag variables used in destructuring, optional chaining, or TypeScript type‑only imports, and how to diagnose and fix it.
09 Jun 2026, 18:29 UTC

Recognizable Condition
ESLint reports a variable as unused even though the variable is referenced in the code. Typical patterns that trigger this false positive include:
- Destructuring assignments where the variable appears on the left‑hand side but is used later.
- Optional chaining calls (e.g.,
obj?.prop) where the variable is part of the chain. - Type‑only imports in TypeScript (
import type { Foo } from './mod') where the imported name is used only in type positions.
Cause/Diagnostic Table
| Possible Cause | Why It Leads to a False Positive |
|---|---|
| ESLint version < 8.0 | The no-unused-vars rule did not treat optional chaining as usage. |
parserOptions.ecmaVersion set below 2022 | Older ECMAScript limits the parser’s understanding of modern syntax. |
Missing or misconfigured @typescript-eslint/parser | TypeScript‑specific constructs (type‑only imports, const assertions) are not recognized as usage. |
Inline eslint-disable comments suppressing the rule | The rule is turned off for the line or block, hiding both real and false positives. |
| Using the default ESLint parser with TypeScript files | The parser cannot understand TypeScript syntax, so it treats type‑only references as unused. |
Ordered Checks
- Confirm ESLint version
# Run in the project root npm list eslint # or yarn list eslintExpected output: version >= 8.0.0. If lower, upgrade (
npm install eslint@latest --save-dev). - Verify parser options
Open
.eslintrc.js,.eslintrc.json, oreslint.config.jsand ensure:{ "parserOptions": { "ecmaVersion": 2022, "sourceType": "module" } }If missing, add or update the fields.
- Ensure TypeScript parser and plugin are installed
npm install --save-dev @typescript-eslint/parser @typescript-eslint/eslint-pluginThen add to config:
{ "parser": "@typescript-eslint/parser", "plugins": ["@typescript-eslint"], "extends": ["plugin:@typescript-eslint/recommended"] } - Check for suppressing comments
Search the file for
eslint-disableoreslint-disable-next-linethat mentionsno-unused-vars. Remove them if they are not needed. - Run debug to see active parser and rules
# Replace with the path to the problematic source npx eslint --debug 2>&1 | grep -E "(parser|rule:no-unused-vars)"Look for lines indicating which parser is used and whether
no-unused-varsis active.
Fixes Tied to Findings
- Upgrade ESLint – if version < 8.0, run
npm install eslint@latest --save-dev. - Set modern ECMAScript target – ensure
parserOptions.ecmaVersionis 2022 or higher. - Add TypeScript parser – install
@typescript-eslint/parserand@typescript-eslint/eslint-plugin, then configure as shown above. - Remove unnecessary disables – delete inline
eslint-disablelines that only silenceno-unused-vars. - Re‑run lint – after changes, execute
npx eslint src/**/*.{js,ts}and confirm the warning disappears.
Escalation Criteria
If the warning persists after applying the above fixes:
- Check for custom
no-unused-varsoverrides inoverridessections of the config that may reset the rule. - Verify that no other plugin (e.g.,
eslint-plugin-import) is interfering with the rule’s execution. - Run
npx eslint --print-configto inspect the final resolved configuration for the file. - If the issue remains, consider filing a reproduction in the ESLint repository with the exact code snippet and config.
Throughout this process, keep the project under version control so you can revert any configuration changes if new lint errors appear.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.