javascript - ESLint activate option to notify "Object is possibly null" - Stack Overflow

For some reason, my .eslintrc.json has some option, or has a lack of some option to catch the warningOb

For some reason, my .eslintrc.json has some option, or has a lack of some option to catch the warning

Object is possibly 'null'

What I need to change to eslint notify my about this warning?

IMPORTANT this is on my unit tests, so adding strictNullChecks does not work for my project, that why I asked if eslint has an option for that.

{
  "settings": {
    "react": {
      "version": "latest"
    }
  },
  "env": {
    "browser": true,
    "es2021": true
  },
  "extends": ["eslint:remended", "plugin:react/remended", "plugin:@typescript-eslint/remended", "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/*"]
}

My tsconfig.json

{
  "exclude": ["lib/**/*.spec.tsx", "lib/**/*.stories.tsx", "lib/**/*.styles.tsx", "settings/**/*", "dist/**/*"],
  "pilerOptions": {
    "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
  }
}

For some reason, my .eslintrc.json has some option, or has a lack of some option to catch the warning

Object is possibly 'null'

What I need to change to eslint notify my about this warning?

IMPORTANT this is on my unit tests, so adding strictNullChecks does not work for my project, that why I asked if eslint has an option for that.

{
  "settings": {
    "react": {
      "version": "latest"
    }
  },
  "env": {
    "browser": true,
    "es2021": true
  },
  "extends": ["eslint:remended", "plugin:react/remended", "plugin:@typescript-eslint/remended", "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/*"]
}

My tsconfig.json

{
  "exclude": ["lib/**/*.spec.tsx", "lib/**/*.stories.tsx", "lib/**/*.styles.tsx", "settings/**/*", "dist/**/*"],
  "pilerOptions": {
    "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
  }
}
Share Improve this question edited Oct 15, 2021 at 20:39 Rodrigo asked Oct 15, 2021 at 19:53 RodrigoRodrigo 56116 gold badges76 silver badges162 bronze badges 1
  • 1 It is a typescript thing, you must enable it in tsconfig.json (strict option) – Akxe Commented Oct 15, 2021 at 20:05
Add a ment  | 

3 Answers 3

Reset to default 4

In your tsconfig.json file under "pilerOptions", you want to set "strictNullChecks" to "true":

{
    "pilerOptions": {
        "strictNullChecks": true,
    }
}

That will make typescript warn you when a value is possibly null. I don't think you can make eslint do it for you.

The problem was on the line:

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

Because of that, the tests are being ignored.

ESLint cannot lint or warn about types only typescript can do it.

You want to have 2 tsconfig.json fles, tsconfig.test.json and tsconfig.json

The tsconfig.json should be like:

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

and the tsconfig.test.json should be like:

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

This way the script null check will not be checked during test run.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745366861a4624620.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信