How to disable warn about some unused params, but keep "@typescript-eslint/no-unused-vars" rule Ask Question

How to disable warn about some unused params, but keep

I want to disable no unused params warning in some cases but keep "unused vars" rule.

For example here I would want to leave arguments in place to see what is passed to resolver:

const Query = objectType({
  name: 'Query',
  definition(t) {
    t.field('test', {
      type: 'Test',
      resolve: (root, args, ctx) => {
        const x = 1

        return { id: 1, time: new Date().toString() }
      },
    })
  },
})

I get warnings:

26:17  warning  'root' is defined but never used        @typescript-eslint/no-unused-vars
26:23  warning  'args' is defined but never used        @typescript-eslint/no-unused-vars
26:29  warning  'ctx' is defined but never used         @typescript-eslint/no-unused-vars
27:15  warning  'x' is assigned a value but never used  @typescript-eslint/no-unused-vars

ESLint config:

module.exports = {
  root: true,
  parser: '@typescript-eslint/parser',
  parserOptions: { ecmaVersion: 2020, ecmaFeatures: { jsx: true } },
  env: {
    browser: true,
    node: true,
  },
  extends: ['plugin:react-hooks/recommended', 'eslint:recommended', 'plugin:@typescript-eslint/recommended', 'plugin:react/recommended'],
  settings: {
    react: {
      version: 'detect',
    },
  },
  rules: {
    '@typescript-eslint/no-empty-function': 'off',
    'react/react-in-jsx-scope': 'off',
    '@typescript-eslint/no-explicit-any': 'off',
    'react/prop-types': 'off',
    '@typescript-eslint/no-var-requires': 'off',
    '@typescript-eslint/explicit-module-boundary-types': 'off',
    'no-unused-vars': 'off',
    '@typescript-eslint/no-unused-vars': ['off'],
  },
  ignorePatterns: ['**/generated/*'],
}

I was trying to disable it somehow, but found only this option that disables everything:

'no-unused-vars': 'off',
'@typescript-eslint/no-unused-vars': ['off'],

ベストアンサー1

Only way that I found is to use ignore pattern argsIgnorePatternルール オプションで、変数が使用されていない場合は、アンダースコアを追加するだけで_ctxESLint はそれを無視しますが、no-unused-varsルールは他の値に対しては引き続き機能します。この値を使用する必要がある場合は、アンダースコアを削除するだけですctx

// .eslintrc.json
{
  // ...
  "rules": {
    // note you must disable the base rule
    // as it can report incorrect errors
    "no-unused-vars": "off",
    "@typescript-eslint/no-unused-vars": [
      "warn", // or "error"
      {
        "argsIgnorePattern": "^_",
        "varsIgnorePattern": "^_",
        "caughtErrorsIgnorePattern": "^_"
      }
    ]
  }
}

^_RegExp を使用して、このパターンを自由に変更できます。

例:

const _a = 'unused, with underscore, no warning'
const b = 'unused, no underscore, warning'

おすすめ記事