文档管理中心
指南编写与调试应用代码编辑代码检查Code Linter代码检查规则通用规则@typescript-eslint@typescript-eslint/strict-boolean-expressions

@typescript-eslint/strict-boolean-expressions

不允许在布尔表达式中使用非布尔类型。

规则配置

收起
自动换行
深色代码主题
复制
  1. // code-linter.json5
  2. {
  3. "rules": {
  4. "@typescript-eslint/strict-boolean-expressions": "error"
  5. }
  6. }

正例

收起
自动换行
深色代码主题
复制
  1. // nullable values should be checked explicitly against null or undefined
  2. function getNum(): number | undefined {
  3. return undefined;
  4. }
  5. const num: number | undefined = getNum();
  6. if (num !== undefined) {
  7. console.log('num is defined');
  8. }
  9. function getStr(): string | null {
  10. return 'null';
  11. }
  12. const str: string | null = getStr();
  13. if (str !== null) {
  14. console.log('str is not empty');
  15. }

反例

收起
自动换行
深色代码主题
复制
  1. // nullable values should be checked explicitly against null or undefined
  2. function getNum(): number | undefined {
  3. return undefined;
  4. }
  5. const num: number | undefined = getNum();
  6. if (num) {
  7. console.log('num is defined');
  8. }
  9. function getStr(): string | null {
  10. return 'null';
  11. }
  12. const str: string | null = getStr();
  13. if (str) {
  14. console.log('str is not empty');
  15. }

规则集

收起
自动换行
深色代码主题
复制
  1. plugin:@typescript-eslint/all

Code Linter代码检查规则的配置指导请参考Code Linter代码检查

在 指南 中进行搜索
请输入您想要搜索的关键词