ESLint の no-unused-vars が無視できない
no-unused-vars は変数・引数・型・ファイル単位で抑制方法が違う。
アンダースコア prefix / disable-next-line / overrides を使い分け、意図した未使用だけ通す設定にする。
公開: 更新:
npx eslint app.js /tmp/tmp.5FIkE0epbr/app.js 1:28 error 'next' is defined but never used no-unused-vars ✖ 1 problem (1 error, 0 warnings)
npx eslint app.js
2026-11-14 に node:20(ESLint 9.39.5 / Node.js 20.20.2) で実際に打って取った出力。検証環境の詳細
要約
no-unused-vars の抑制は「変数」「引数」「型」「ファイル単位」で方法が異なる。
公式の no-unused-vars ルール定義(新しいタブで開く) に従い、argsIgnorePattern で _ prefix を許可するのが最も標準的な書き方である。
実行例
node:20 環境で上記の手順を動かすと、argsIgnorePattern 未設定の状態では 'next' is defined but never used が 1 エラーとして検出され、終了コード 1 になった。_ prefix のパターンを設定した後も、eslint-disable-next-line を適用した後も、終了コードは 0 に変わった。
記事で説明した 2 つの抑制手段は、どちらも効いた。
v9.39.5$ npx eslint app.js
/tmp/tmp.5FIkE0epbr/app.js
1:28 error 'next' is defined but never used no-unused-vars
✖ 1 problem (1 error, 0 warnings)
$ echo $?
1$ npx eslint app.js
$ echo $?
0$ npx eslint app.js
$ echo $?
0— 2026-11-14 時点の出力
検証環境Node.js 20.20.2 / npm 10.8.2 / Python 3.11.2 / 2026-11-14 検証(ほか 2 件)
- 検証日
- 実行環境
node:20Debian GNU/Linux 12 (bookworm)- バージョン
- Node.js 20.20.2
- npm 10.8.2
- Python 3.11.2
- Git 2.39.5
- ESLint 9.39.5
この記事の「実行例」は、上記の環境で実際にコマンドを実行して得られた出力をそのまま掲載しています。 再現手順はリポジトリの検証スクリプトとして管理し、定期的に再実行して出力を更新しています。
解決策
1. argsIgnorePattern で _ prefix を許可
// eslint.config.js
export default [
{
rules: {
"no-unused-vars": [
"error",
{
argsIgnorePattern: "^_",
varsIgnorePattern: "^_",
caughtErrorsIgnorePattern: "^_",
},
],
},
},
];これで (req, res, _next) => {} のように _next と書けば警告されない。
2. TypeScript では typescript-eslint のルールに置き換える
rules: {
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": "error",
}typescript-eslint の no-unused-vars(新しいタブで開く) は、型の中の引数名を未使用と判定しない。
型注釈で使っていれば、import { User } でも import type { User } でも未使用にならない。
どこにも使わない import は、どちらの書き方でも未使用と判定される(ESLint 10.14.0 と typescript-eslint 8.72.1 で確認)。
3. ファイル単位の overrides
// eslint.config.js
export default [
{ rules: { "no-unused-vars": "error" } },
{
files: ["**/*.test.ts", "**/*.spec.ts"],
rules: { "no-unused-vars": "off" },
},
];4. disable コメントを正しく使う
// eslint-disable-next-line no-unused-vars
const unused = 1;
/* eslint-disable no-unused-vars */
const a = 1;
const b = 2;
/* eslint-enable no-unused-vars */disable-next-line は直後 1 行に効き、disable ... enable で囲むとブロック全体に効く。
乱用するとレビューの意味が薄れるので、原則は _ prefix で意図を示すのが望ましい。
よくある原因
- 未使用引数を抑制する設定が無い。
コールバックで(req, res, next)を全部受け取りたい場合、argsIgnorePatternを指定しないとnextで怒られる。 - TypeScript で ESLint 本体のルールを使っている。
interfaceのメソッドや関数のオーバーロードに書いた引数名まで、未使用と判定される。 - テストファイルだけ別ルールにしていない。
テスト用ヘルパで未使用関数を残したいときも、共通ルールが効いてしまう。 - disable コメントの位置を誤っている。
eslint-disable-next-lineが対象にするのは 直後 1 行のみ で、ブロック全体には効かない。