代码之家  ›  专栏  ›  技术社区  ›  Rodrigo

ESLint激活选项以通知“对象可能为空”

  •  0
  • Rodrigo  · 技术社区  · 4 年前

    出于某种原因,我的 .eslintrc.json 有一些选择,或者缺乏一些选择来捕捉警告

    对象可能为“null”

    我需要将哪些内容更改为eslint,以通知我此警告?

    重要的 这是在我的单元测试中,所以添加 strictNullChecks 不适用于我的项目,这就是为什么我问eslint是否有这个选项。

    {
      "settings": {
        "react": {
          "version": "latest"
        }
      },
      "env": {
        "browser": true,
        "es2021": true
      },
      "extends": ["eslint:recommended", "plugin:react/recommended", "plugin:@typescript-eslint/recommended", "prettier"],
      "parser": "@typescript-eslint/parser",
      "parserOptions": {
        "ecmaFeatures": {
          "jsx": true
        },
        "ecmaVersion": 12,
        "sourceType": "module"
      },
      "plugins": ["react", "@typescript-eslint"],
      "rules": {
        "@typescript-eslint/no-inferrable-types": "off"
      },
      "ignorePatterns": ["dist/*"]
    }
    

    我的 tsconfig.json

    {
      "exclude": ["lib/**/*.spec.tsx", "lib/**/*.stories.tsx", "lib/**/*.styles.tsx", "settings/**/*", "dist/**/*"],
      "compilerOptions": {
        "skipLibCheck": true,
        "target": "es5",
        "lib": ["es5", "dom"],
        "jsx": "react",
        "declaration": true,
        "declarationMap": true,
        "sourceMap": true,
        "outDir": "dist",
        "rootDirs": ["lib"],
        "strict": true,
        "moduleResolution": "node",
        "esModuleInterop": true,
        "forceConsistentCasingInFileNames": true,
        "strictNullChecks": true
      }
    }
    
    1 回复  |  直到 4 年前
        1
  •  4
  •   Jonathan Potter    4 年前

    在“compilerOptions”下的tsconfig.json文件中,您要将“strictNullChecks”设置为“true”:

    {
        "compilerOptions": {
            "strictNullChecks": true,
        }
    }
    

    这将使typescript在值可能为null时警告您。我认为你不能让eslint为你做这件事。

        2
  •  2
  •   Rodrigo    4 年前

    问题就在眼前:

    "exclude": ["lib/**/*.spec.tsx", ...
    

    因此,这些测试被忽略了。

        3
  •  1
  •   Akxe    4 年前

    ESLint不能lint或警告只有typescript才能做到的类型。

    你想要两个 tsconfig.json 弗勒斯, tsconfig.test.json tsconfig.json

    这个 tsconfig.json 应该是这样的:

    {
        ...
        "compilerOptions": {
            "strictNullChecks": true,
        }
    }
    

    以及 tsconfig.test.json 应该是这样的:

    {
        "extends": "./tsconfig.json",
        ...
        "compilerOptions": {
            "strictNullChecks": false,
        }
    }
    

    这样,脚本空检查将 在试运行期间进行检查。