Skip to content
← Back to rules

typescript/strict-boolean-expressions Pedantic

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

무엇을 하나요

Disallow certain types in boolean expressions.

왜 문제인가요?

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:

  • Arguments to the !, &&, and || operators
  • The condition in a conditional expression (cond ? x : y)
  • Conditions for if, for, while, and do-while statements.

예시

이 규칙에서 올바르지 않은 코드 예:

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";

이 규칙에서 올바른 코드 예:

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");
}

설정

이 규칙은 다음 속성을 갖는 설정 객체를 허용합니다.

allowAny

타입: boolean

기본값: false

Whether to allow any type in boolean contexts.

allowNullableBoolean

타입: boolean

기본값: false

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

allowNullableEnum

타입: boolean

기본값: false

Whether to allow nullable enum types in boolean contexts.

allowNullableNumber

타입: boolean

기본값: false

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

allowNullableObject

타입: boolean

기본값: true

Whether to allow nullable object types in boolean contexts.

allowNullableString

타입: boolean

기본값: false

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

allowNumber

타입: boolean

기본값: true

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

allowString

타입: boolean

기본값: true

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

사용 방법

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

json
{
  "options": {
    "typeAware": true
  },
  "rules": {
    "typescript/strict-boolean-expressions": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  options: { typeAware: true },
  rules: {
    "typescript/strict-boolean-expressions": "error",
  },
});
bash
oxlint --type-aware --deny typescript/strict-boolean-expressions

버전

이 규칙은 v1.25.0에 추가되었습니다.

참고