reactjs React + Typescript + ESLint:no-unused-vars仅用于导出的Enum-s

o3imoua4  于 12个月前  发布在  React
关注(0)|答案(2)|浏览(82)

我已经配置了一个基本的React-Typescript项目(由React-react-app创建)。
对于以下文件:MeetingLevels.ts

enum MeetingLevels {
  SiteLeader = 1,
  AreaManager = 2,
  GroupLeader = 3,
  TeamLeader = 4,
}

export default MeetingLevels;

.. npm run lint产生以下错误:

C:\...\models\MeetingLevels.ts
  1:6  warning  'MeetingLevels' is defined but never used  no-unused-vars
  2:3  warning  'SomeLeader' is defined but never used     no-unused-vars
  3:3  warning  'SpaceManager' is defined but never used   no-unused-vars
  4:3  warning  'GroupLeader' is defined but never used    no-unused-vars

但它被用在了这个项目的多个地方。(在IntelliJ IDEA中Shift+单击也可以找到它)-例如:SomeReactFile.tsx

import MeetingLevels from "../../models/MeetingLevels";
 ...

  const mapMeetingButton = (level: number): string => {
    switch (level) {
      case MeetingLevels.SomeLeader:
        return "A_CUSTOM_STRING";
      case MeetingLevels.SpaceManager:
        return "SOME_OTHER_CUSTOM_STRING";

我试图解决这个问题,但显然,我的.eslintrc.js似乎很好。到目前为止,我所尝试的都不起作用,这个ESLint错误只存在于这里。(这是我在项目中唯一的enum
.eslintrc.js

module.exports = {
  env: {
    browser: true, // Browser global variables like `window` etc.
    commonjs: true, // CommonJS global variables and CommonJS scoping.Allows require, exports and module.
    jest: true, // Jest global variables like `it` etc.
    node: true, // Defines things like process.env when generating through node
  },
  extends: [
    "plugin:react/jsx-runtime",
    "plugin:react/recommended", // React recommended rule set
    "eslint:recommended",
    "plugin:@typescript-eslint/eslint-recommended", // Enables few key rules in ESLint rule book
    "plugin:@typescript-eslint/recommended", // TypeScript ESLint compatibility plugin
  ],
  parser: "@typescript-eslint/parser", // Recommended parser for Typescript based React project
  parserOptions: {
    ecmaFeatures: {
      jsx: true,
    },
    ecmaVersion: "latest", // Allows for the parsing of modern ECMAScript features
    sourceType: "module", // Allows for the use of imports
  },
  plugins: [
    "import", // eslint-plugin-import plugin. https://www.npmjs.com/package/eslint-plugin-import
    "@typescript-eslint", // TypeScript official plugin
    "react", // React official plugin
    "react-hooks", // React plugin extension for using React Hooks
  ],
  root: true, // For configuration cascading.
  rules: {
    "react/jsx-uses-react": "error",
    "react/jsx-uses-vars": "error",
    camelcase: "error",
    "import/order": [
      "warn",
      {
        alphabetize: {
          caseInsensitive: true,
          order: "asc",
        },
        groups: [
          "builtin",
          "external",
          "index",
          "sibling",
          "parent",
          "internal",
        ],
      },
    ],
    "max-len": [
      "warn",
      {
        code: 120,
      },
    ],
    quotes: ["warn", "double"],
    "react/jsx-indent-props": ["error", 2],
    "react/prop-types": "warn",
    "react/react-in-jsx-scope": "off",
    "sort-imports": [
      "warn",
      {
        ignoreCase: false,
        ignoreDeclarationSort: true,
        ignoreMemberSort: false,
      },
    ],
    "sort-keys": [
      "warn",
      "asc",
      {
        caseSensitive: true,
        minKeys: 2,
        natural: false,
      },
    ],
    "@typescript-eslint/ban-types": ["warn"],
    "@typescript-eslint/no-empty-function": ["warn"],
    "@typescript-eslint/no-empty-interface": ["warn"],
    "no-console": "warn",
    "no-constant-condition": ["warn"],
    "no-duplicate-imports": "warn",
    "no-unused-vars": "warn",
  },
  settings: {
    react: {
      version: "detect", // Detect react version
    },
  },
};
yrdbyhpb

yrdbyhpb1#

您可以禁用默认的no-unused-vars规则,而使用@typescript-eslint/no-unused-vars(如果未启用):

"@typescript-eslint/no-unused-vars": "warn",
"no-unused-vars": "off"
tquggr8v

tquggr8v2#

我想首先导出枚举并将其导入到任何你想要的地方,就像这样:

export enum MeetingLevels {
      SiteLeader = 1,
      AreaManager = 2,
      GroupLeader = 3,
      TeamLeader = 4,
    }

并导入:

import { MeetingLevels } from "../to-the-target";

相关问题