You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决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.json to ignore unused parameters marked with underscores, or adjust rules to fit your needs. Modify the rules section:
    "rules": {
      "@typescript-eslint/no-var-requires": "off",
      "@typescript-eslint/no-unused-vars": [
        "error",
        {
          "argsIgnorePattern": "^_",
          "varsIgnorePattern": "^_",
          "args": "all"
        }
      ]
    }
    
    This setup lets you use underscores to flag unused parameters without disabling the rule entirely.

Content sourced from Stack Exchange, question author: Liben Hailu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 15:00:34