首页 > 解决方案 > 为什么 eslint 忽略了我的 strictNullChecks 设置?

问题描述

我正在尝试将现有的大型 JavaScript 代码库移植到 TypeScript。我已经安装typescript并配置了项目以便@typescript-eslint/eslint-plugin使用@typescript-eslint/parser它。devDependenciestsconfig.json

我的(最小)tsconfig.json是:

{
  "compilerOptions": {
    "target": "es5",
    "module": "esnext",
    "alwaysStrict": true,
    "strict": true,
    "strictNullChecks": true
  },
}

在现有的 .tsx 文件上运行 eslint(正确)会显示一些错误:

$ ./node_modules/.bin/eslint ./src/components/JustSomeComponent.tsx 

/src/components/JustSomeComponent.tsx
   57:75  error    Unexpected empty arrow function 'myFunction'                               @typescript-eslint/no-empty-function
   ...

✖ 20 problems (8 errors, 12 warnings)
  4 errors and 0 warnings potentially fixable with the `--fix` option.

但是,我想确保strictNullChecks确实启用了该设置,所以我创建了一个新文件并故意犯了一个错误:

import React from 'react';

export default class MyComponent extends React.Component {
  render(): React.ReactNode {
    const x: number = null; // I would expect an error here!
    console.log(x);
    return null;
  }
}

但是对此文件运行检查不会返回错误:

$ ./node_modules/.bin/eslint ./src/components/MyComponent.tsx
$

删除render()方法的返回类型会导致错误,因此会执行检查 - 我只是没有收到有关无效分配的警告null

知道如何启用strictNullChecks,或者至少如何调试它被禁用的原因吗?

标签: reactjstypescripteslint

解决方案


strictNullChecksTypeScript功能,而不是ESLint功能。


推荐阅读