Skip to content
← Back to rules

typescript/no-unnecessary-boolean-literal-compare Suspicious

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

何をするか

このルールは、不要な真偽値リテラルとの等値比較を禁止します。

なぜ問題なのか

真偽値の変数を真偽値リテラルと比較することは、比較を省略できる場合に不要です。このような比較はコードを冗長にし、価値を追加しません。

このルールに対して誤りなコードの例:

ts
declare const someCondition: boolean;

if (someCondition === true) {
  // ...
}

if (someCondition === false) {
  // ...
}

if (someCondition !== true) {
  // ...
}

if (someCondition !== false) {
  // ...
}

const result = someCondition == true;

このルールに対して正しいコードの例:

ts
declare const someCondition: boolean;

if (someCondition) {
  // ...
}

if (!someCondition) {
  // ...
}

// 真偽値以外の型との比較は許可される
declare const someValue: unknown;
if (someValue === true) {
  // ...
}

設定

このルールは以下のプロパティを持つ設定オブジェクトを受け入れます。

allowComparingNullableBooleansToFalse

type: boolean

default: true

null を含む真偽値式を false と比較することを許可するかどうか。 false の場合、x === false(x が boolean | null)が警告対象になります。

allowComparingNullableBooleansToTrue

type: boolean

default: true

null を含む真偽値式を true と比較することを許可するかどうか。 false の場合、x === true(x が boolean | null)が警告対象になります。

使用方法

このルールを有効にするには、設定ファイルまたは CLI で次のように使用できます:

json
{
  "rules": {
    "typescript/no-unnecessary-boolean-literal-compare": "error"
  }
}
bash
oxlint --type-aware --deny typescript/no-unnecessary-boolean-literal-compare

参照