简体   繁体   English

为什么我的 switch-exhaustiveness-check lint 规则不起作用?

[英]Why is my switch-exhaustiveness-check lint rule not working?

The following code is triggering a standard default-case lint error, but the @typescript-eslint/switch-exhaustiveness-check is not being triggered.以下代码触发了标准的default-case lint 错误,但未触发@typescript-eslint/switch-exhaustiveness-check Any idea what could be causing this?知道是什么原因造成的吗? Other @typescript-eslint rules work fine.其他 @typescript-eslint 规则工作正常。

type Day = 'Monday' | 'Tuesday' | 'Wednesday' | 'Thursday' | 'Friday' | 'Saturday' | 'Sunday';

const day = 'Monday' as Day;
let result = 0;

switch (day) {
  case 'Monday': {
    result = 1;
    break;
  }
}

Here is my.eslintrc.js这是 my.eslintrc.js

module.exports = {
  env: {
    browser: true,
    es2021: true,
  },
  extends: [
    'plugin:react/recommended',
    'airbnb',
    'plugin:@typescript-eslint/recommended',
    'plugin:prettier/recommended',
  ],
  ignorePatterns: ['.eslintrc.js'],
  parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaFeatures: {
      jsx: true,
    },
    ecmaVersion: 'latest',
    sourceType: 'module',
    project: ['./tsconfig.json'],
  },
  plugins: ['react', '@typescript-eslint', 'react-hooks'],
  // Fix error "Unable to resolve path to module" for tsx files
  settings: {
    'import/resolver': {
      typescript: {},
    },
  },
  rules: {
    // Fix "React used before defined" error
    'no-use-before-define': 'off',
    '@typescript-eslint/no-use-before-define': ['error'],

    // Allow jsx
    'react/jsx-filename-extension': ['warn', { extensions: ['.tsx'] }],

    // Fix error "Missing file extension tsx"
    'import/extensions': [
      'error',
      'ignorePackages',
      {
        ts: 'never',
        tsx: 'never',
      },
    ],

    // Fix error "‘Enum’ is already declared in the upper scope"
    'no-shadow': 'off',
    '@typescript-eslint/no-shadow': ['error'],

    // Add rules for react hooks
    'react-hooks/rules-of-hooks': 'error',
    'react-hooks/exhaustive-deps': 'warn',

    // Use 120 character windows
    'max-len': [
      'error',
      {
        code: 120,
        ignoreUrls: true,
        ignoreStrings: true,
        ignoreTemplateLiterals: true,
        ignoreRegExpLiterals: true,
      },
    ],

    // Stop using default export
    'import/no-default-export': 'error',
    'import/prefer-default-export': 'off',

    // Since we use interfaces often in DDD and sometimes an implemented function uses this and sometimes not, it makes
    // this rule impractical.
    'class-methods-use-this': 'off',

    // Require accessibility modifiers on everything except public constructors
    '@typescript-eslint/explicit-member-accessibility': ['error', { overrides: { constructors: 'no-public' } }],

    // We use these many times in domain objects, so it makes sense to ignore them.
    '@typescript-eslint/no-empty-interface': 'off',

    // Allow test dependencies to appear in devDependencies
    'import/no-extraneous-dependencies': ['error', { devDependencies: ['**/*.test.ts', '**/*.test.tsx'] }],

    // Place default props inside class field
    'react/static-property-placement': ['error', 'static public field'],

    '@typescript-eslint/switch-exhaustiveness-check': ['error'],
  },
};

error isn't actually a supported value for the exhaustiveness-check rule, only warn . error实际上不是穷举检查规则支持的值,只有warn Here's the documentation with accepted values:https://typescript-eslint.io/rules/switch-exhaustiveness-check/这是具有可接受值的文档:https://typescript-eslint.io/rules/switch-exhaustiveness-check/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM