Rajat Jaswal
Rajat Jaswal

Reputation: 11

On creating Next.js build getting warning for no-unused-vars with configured eslint

Warning: 'myparam' is defined but never used.  no-unused-vars

I am getting above warning in my file below I have a store implenting of zustand, however I think the warning is not related to zustand but because of eslint rules:

import { create } from 'zustand';


export type MyStoreState = {
  myVar: string[];
  myFunction: (
    myparam: string[],
  ) => void;
}

const useMyStore = create<MyStoreState>((set) => ({
  myVar: [],
  myFunction: (myparam) => {
    set({
      myVar: myparam,
    });
  }
}));

export default useMyStore;

Below is my .eslintrc.js file:

module.exports = {
  parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaVersion: 2021,
    sourceType: 'module',
    ecmaFeatures: {
      jsx: true,
    },
  },
  env: {
    browser: true,
    es2021: true,
    node: true,
  },
  root: true,
  extends: [
    'next',
    'eslint:recommended',
    'prettier',
    'next/core-web-vitals',
    'plugin:@typescript-eslint/recommended',
    'plugin:react/recommended',
    'plugin:prettier/recommended',
    'plugin:react-hooks/recommended',
    'plugin:@typescript-eslint/recommended',
  ],
  plugins: ['prettier', '@typescript-eslint', 'react', 'react-hooks'],
  rules: {
    // JavaScript rules
    'prefer-const': 'warn',
    'no-var': 'warn',
    'no-unused-vars': 'warn',
    'object-shorthand': 'warn',
    'quote-props': ['warn', 'as-needed'],
    // TypeScript rules
    '@typescript-eslint/no-unused-vars': ['off'],
    '@typescript-eslint/no-explicit-any': 'off',
    '@typescript-eslint/array-type': [
      'warn',
      {
        default: 'array',
      },
    ],
    '@typescript-eslint/consistent-type-assertions': [
      'warn',
      {
        assertionStyle: 'as',
        objectLiteralTypeAssertions: 'never',
      },
    ],
    // React rules
    'react/jsx-fragments': ['warn', 'syntax'], // Shorthand syntax for React fragments
    'react/jsx-filename-extension': [
      'warn',
      {
        extensions: ['ts', 'tsx'],
      },
    ],
    'react-hooks/rules-of-hooks': 'error', // Checks rules of Hooks
    'react-hooks/exhaustive-deps': 'warn', // Checks effect dependencies
    'react/react-in-jsx-scope': 'off',
    'react/prop-types': 'off',
    'prettier/prettier': 'warn',
  },
  settings: {
    react: {
      version: 'detect',
    },
  },
};

Please help I want to know why it is triggering the warning of "no-unused-vars" when I am using that in the function below:

myFunction: (myparam) => {
    set({
      myVar: myparam,
    });
  }

The "no-unused-vars" warning is coming for myparam inside type MyStoreType.

Please help, myparam in the type declaration is not meant to be used why the eslint is giving me warning for that.

I tried to disable "no-unused-vars" rule from .eslintrc but that is not the solution.

Upvotes: 1

Views: 1394

Answers (1)

Sukka
Sukka

Reputation: 332

TL; DR

{
  "rules": {
-   "no-unused-vars": 'warn',
+   "no-unused-vars": "off",
+   "@typescript-eslint/no-unused-vars": "warn"
  }
}

ESLint's built-in no-unused-vars works great with JavaScript projects but not TypeScript projects (can't handle type declaration properly).

The @typescript-eslint/eslint-plugin provides a rule @typescript-eslint/no-unused-vars that can work with TypeScript projects. The @typescript-eslint:recommended preset will automatically disable no-unused-vars and enable @typescript-eslint/no-unused-vars.

Upvotes: 4

Related Questions