如何解决Next.js中Eslint no-unused-vars类型参数报错?
Solution for ESLint
no-unused-vars on TypeScript Function Parameter Problem
I'm building a Next.js project with profile view, delete, and update features. I defined this type for my profile component's props:
type DeleteProfileParams = { // ESLint error: 'profile' is defined but never used. (no-unused-vars) onSuccess?: (profile: Profile) => void }
When the delete operation succeeds, I call the parent-passed onSuccess function with the API's returned result. But ESLint throws a no-unused-vars error for the profile parameter. I don't want to disable the rule globally or per-line (even though line-disabling works), and I'm looking for a better approach. Here's my .eslint.json config:
{ "env": { "browser": true, "es2021": true, "node": true }, "parser": "@typescript-eslint/parser", "extends": [ "plugin:@typescript-eslint/recommended", "next/core-web-vitals", "eslint:recommended", "plugin:react/jsx-runtime", "plugin:react/jsx-runtime", "prettier" ], "globals": { "React": "readonly" }, "rules": { "@typescript-eslint/no-var-requires": "off" }, "overrides": [ { "files": [ "*.test.{ts,tsx}", "src/testing/**/*.{ts,tsx}" ], "globals": { "it": "readonly", "describe": "readonly" }, "rules": { "@typescript-eslint/no-explicit-any": "off", "@typescript-eslint/ban-ts-comment": "off" } } ] }
Solutions
- Prefix with underscore: Rename the parameter to
_profile—TypeScript and ESLint recognize this as an intentionally unused parameter, suppressing the error:type DeleteProfileParams = { onSuccess?: (_profile: Profile) => void } - Tweak ESLint rule configuration: Update your
.eslint.jsonto ignore unused parameters marked with underscores, or adjust rules to fit your needs. Modify therulessection:
This setup lets you use underscores to flag unused parameters without disabling the rule entirely."rules": { "@typescript-eslint/no-var-requires": "off", "@typescript-eslint/no-unused-vars": [ "error", { "argsIgnorePattern": "^_", "varsIgnorePattern": "^_", "args": "all" } ] }
Content sourced from Stack Exchange, question author: Liben Hailu
相关产品推荐
相关产品推荐

