Skip to content
← 回到规则

typescript/strict-boolean-expressions Pedantic

💭 This rule requires type information.
🚧 An auto-fix is planned for this rule, but not implemented at this time.

它的作用

🌐 What it does

在布尔表达式中禁止某些类型。

🌐 Disallow certain types in boolean expressions.

这为什么不好?

🌐 Why is this bad?

禁止在期望布尔值的表达式中使用非布尔类型。booleannever 类型始终允许。可以通过选项配置其他在布尔上下文中被认为安全的类型。

🌐 Forbids usage of non-boolean types in expressions where a boolean is expected. boolean and never types are always allowed. Additional types which are considered safe in a boolean context can be configured via options.

已检查以下节点:

🌐 The following nodes are checked:

  • !&&|| 运算符的参数
  • 条件表达式中的条件(cond ? x : y
  • ifforwhiledo-while 语句的条件。

例子

🌐 Examples

此规则的 错误 代码示例:

🌐 Examples of incorrect code for this rule:

ts
const str = "hello";
if (str) {
  console.log("string");
}

const num = 42;
if (num) {
  console.log("number");
}

const obj = { foo: "bar" };
if (obj) {
  console.log("object");
}

declare const maybeString: string | undefined;
if (maybeString) {
  console.log(maybeString);
}

const result = str && num;
const ternary = str ? "yes" : "no";

此规则的正确代码示例:

🌐 Examples of correct code for this rule:

ts
const str = "hello";
if (str !== "") {
  console.log("string");
}

const num = 42;
if (num !== 0) {
  console.log("number");
}

const obj = { foo: "bar" };
if (obj !== null) {
  console.log("object");
}

declare const maybeString: string | undefined;
if (maybeString !== undefined) {
  console.log(maybeString);
}

const bool = true;
if (bool) {
  console.log("boolean");
}

配置

🌐 Configuration

此规则接受一个具有以下属性的配置对象:

🌐 This rule accepts a configuration object with the following properties:

allowAny

类型:boolean

🌐 type: boolean

默认:false

🌐 default: false

是否允许 any 类型在布尔上下文中使用。

🌐 Whether to allow any type in boolean contexts.

allowNullableBoolean

类型:boolean

🌐 type: boolean

默认:false

🌐 default: false

是否允许在布尔上下文中使用可为空的布尔类型(例如,boolean | null)。

🌐 Whether to allow nullable boolean types (e.g., boolean | null) in boolean contexts.

allowNullableEnum

类型:boolean

🌐 type: boolean

默认:false

🌐 default: false

是否允许可为空的枚举类型在布尔上下文中使用。

🌐 Whether to allow nullable enum types in boolean contexts.

allowNullableNumber

类型:boolean

🌐 type: boolean

默认:false

🌐 default: false

是否允许可空数字类型(例如 number | null)在布尔上下文中使用。

🌐 Whether to allow nullable number types (e.g., number | null) in boolean contexts.

allowNullableObject

类型:boolean

🌐 type: boolean

默认:true

🌐 default: true

是否允许在布尔上下文中使用可为空的对象类型。

🌐 Whether to allow nullable object types in boolean contexts.

allowNullableString

类型:boolean

🌐 type: boolean

默认:false

🌐 default: false

是否允许可为空的字符串类型(例如 string | null)在布尔上下文中使用。

🌐 Whether to allow nullable string types (e.g., string | null) in boolean contexts.

allowNumber

类型:boolean

🌐 type: boolean

默认:true

🌐 default: true

是否允许在布尔上下文中使用数字类型(检查非零数字)。

🌐 Whether to allow number types in boolean contexts (checks for non-zero numbers).

allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing

类型:boolean

🌐 type: boolean

默认:false

🌐 default: false

是否允许在未启用 strictNullChecks 的情况下运行此规则。 不建议这样做,因为该规则可能产生不正确的结果。

🌐 Whether to allow this rule to run without strictNullChecks enabled. This is not recommended as the rule may produce incorrect results.

allowString

类型:boolean

🌐 type: boolean

默认:true

🌐 default: true

是否允许在布尔上下文中使用字符串类型(检查非空字符串)。

🌐 Whether to allow string types in boolean contexts (checks for non-empty strings).

如何使用

🌐 How to use

要通过配置文件或命令行启用此规则,你可以使用:

🌐 To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "typescript/strict-boolean-expressions": "error"
  }
}
bash
oxlint --type-aware --deny typescript/strict-boolean-expressions

参考文献

🌐 References