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.

What it does ​

This rule disallows unnecessary equality comparisons with boolean literals.

Why is this bad? ​

Comparing boolean values to boolean literals is unnecessary when the comparison can be eliminated. These comparisons make code more verbose without adding value.

Examples ​

Examples of incorrect code for this rule:

ts
declare const someCondition: boolean;

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

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

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

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

const result = someCondition == true;

Examples of correct code for this rule:

ts
declare const someCondition: boolean;

if (someCondition) {
  // ...
}

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

// Comparisons with non-boolean types are allowed
declare const someValue: unknown;
if (someValue === true) {
  // ...
}

Configuration ​

This rule accepts a configuration object with the following properties:

allowComparingNullableBooleansToFalse ​

type: boolean

default: true

Whether to allow comparing nullable boolean expressions to false. When false, x === false where x is boolean | null will be flagged.

allowComparingNullableBooleansToTrue ​

type: boolean

default: true

Whether to allow comparing nullable boolean expressions to true. When false, x === true where x is boolean | null will be flagged.

How to use ​

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

json
{
  "options": {
    "typeAware": true
  },
  "rules": {
    "typescript/no-unnecessary-boolean-literal-compare": "error"
  }
}
ts
import { defineConfig } from "oxlint";

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

Version ​

This rule was added in v1.12.0.

References ​