ESLint の no-unused-vars が無視できない
no-unused-vars は変数・引数・型・ファイル単位で抑制方法が違う。
アンダースコア prefix / disable-next-line / overrides を使い分け、意図した未使用だけ通す設定にする。
公開:
要約
no-unused-vars の抑制は「変数」「引数」「型」「ファイル単位」で方法が異なる。
公式の no-unused-vars ルール定義(新しいタブで開く) に従い、argsIgnorePattern で _ prefix を許可するのが最も標準的な書き方。
よくある原因
- 未使用引数の抑制設定が無い: コールバックで
(req, res, next)を全部受け取りたい場合、argsIgnorePatternを指定しないとnextで怒られる - 型 import を値 import している: TypeScript で
import { Foo }と書くと値として扱われ、型注釈にしか使っていないと未使用判定される - テストファイルだけ別ルールにしていない: テスト用ヘルパで未使用関数を残したいケースで、共通ルールが効いてしまう
- disable コメントの位置ミス:
eslint-disable-next-lineは 直後 1 行のみ が対象。
ブロック全体には効かない
解決策
1. argsIgnorePattern で _ prefix を許可
// eslint.config.js
export default [
{
rules: {
"no-unused-vars": [
"error",
{
argsIgnorePattern: "^_",
varsIgnorePattern: "^_",
caughtErrorsIgnorePattern: "^_",
},
],
},
},
];これで (req, res, _next) => {} のように _next と書けば警告されない。
2. import type に切り替える
// before
import { User } from "./types";
type Props = { user: User };
// after
import type { User } from "./types";
type Props = { user: User };TypeScript プロジェクトでは typescript-eslint の no-unused-vars(新しいタブで開く) に置き換えると、型 import を正しく扱う。
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 で意図を示すのが望ましい。
実行例
実際に上記の手順を node:20 環境で動かすと、argsIgnorePattern 未設定の状態では 'next' is defined but never used が 1 エラーとして検出され終了コード 1 となる一方、_ prefix パターン設定後および eslint-disable-next-line 適用後はいずれも終了コード 0 に変わり、記事で説明した 2 つの抑制手段がそれぞれ機能することを確認できる。
== versions ==
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
v20.20.2
Python 3.11.2
git version 2.39.5
This is not the tsc command you are looking for
To get access to the TypeScript compiler, tsc, from the command line either:
- Use npm install typescript to first add TypeScript to your project before using npx
- Use yarn to avoid accidentally running code from un-installed packages
v10.6.0
== run ==
--- ESLint を install ---
v9.39.4
--- npx eslint app.js (no-unused-vars: next is defined but never used を想定)---
/tmp/tmp.Qwp7ckO31O/app.js
1:28 error 'next' is defined but never used no-unused-vars
✖ 1 problem (1 error, 0 warnings)
eslint 終了コード: 1
--- 解消1: argsIgnorePattern で _ prefix を許可し、引数を _next に ---
eslint 終了コード: 0(0 なら警告なし)
--- 解消2: eslint-disable-next-line で直後 1 行だけ抑制 ---
eslint 終了コード: 0(0 なら抑制が効いている)— 2026-06-28 時点の出力