1 | # `no-unnecessary-boolean-literal-compare`
|
2 |
|
3 | Flags unnecessary equality comparisons against boolean literals.
|
4 |
|
5 | Comparing boolean values to boolean literals is unnecessary, those comparisons result in the same booleans. Using the boolean values directly, or via a unary negation (`!value`), is more concise and clearer.
|
6 |
|
7 | ## Rule Details
|
8 |
|
9 | This rule ensures that you do not include unnecessary comparisons with boolean literals.
|
10 | A comparison is considered unnecessary if it checks a boolean literal against any variable with just the `boolean` type.
|
11 | A comparison is **_not_** considered unnecessary if the type is a union of booleans (`string | boolean`, `someObject | boolean`).
|
12 |
|
13 | **Warning**: Do not use this rule when `strictNullChecks` is disabled.
|
14 | ESLint is not able to distinguish between `false` and `undefined` or `null` values.
|
15 | This can cause unintended code changes when using autofix.
|
16 |
|
17 | **Note**: Throughout this page, only strict equality (`===` and `!==`) are
|
18 | used in the examples. However, the implementation of the rule does not
|
19 | distinguish between strict and loose equality. Any example below that uses
|
20 | `===` would be treated the same way if `==` was used, and any example below
|
21 | that uses `!==` would be treated the same way if `!=` was used.
|
22 |
|
23 | Examples of code for this rule:
|
24 |
|
25 |
|
26 |
|
27 | ### ❌ Incorrect
|
28 |
|
29 | ```ts
|
30 | declare const someCondition: boolean;
|
31 | if (someCondition === true) {
|
32 | }
|
33 | ```
|
34 |
|
35 | ### ✅ Correct
|
36 |
|
37 | ```ts
|
38 | declare const someCondition: boolean;
|
39 | if (someCondition) {
|
40 | }
|
41 |
|
42 | declare const someObjectBoolean: boolean | Record<string, unknown>;
|
43 | if (someObjectBoolean === true) {
|
44 | }
|
45 |
|
46 | declare const someStringBoolean: boolean | string;
|
47 | if (someStringBoolean === true) {
|
48 | }
|
49 | ```
|
50 |
|
51 | ## Options
|
52 |
|
53 | The rule accepts an options object with the following properties.
|
54 |
|
55 | ```ts
|
56 | type Options = {
|
57 | // if false, comparisons between a nullable boolean variable to `true` will be checked and fixed
|
58 | allowComparingNullableBooleansToTrue?: boolean;
|
59 | // if false, comparisons between a nullable boolean variable to `false` will be checked and fixed
|
60 | allowComparingNullableBooleansToFalse?: boolean;
|
61 | };
|
62 | ```
|
63 |
|
64 | ### Defaults
|
65 |
|
66 | This rule always checks comparisons between a boolean variable and a boolean
|
67 | literal. Comparisons between nullable boolean variables and boolean literals
|
68 | are **not** checked by default.
|
69 |
|
70 | ```ts
|
71 | const defaults = {
|
72 | allowComparingNullableBooleansToTrue: true,
|
73 | allowComparingNullableBooleansToFalse: true,
|
74 | };
|
75 | ```
|
76 |
|
77 | ### `allowComparingNullableBooleansToTrue`
|
78 |
|
79 | Examples of code for this rule with `{ allowComparingNullableBooleansToTrue: false }`:
|
80 |
|
81 |
|
82 |
|
83 | #### ❌ Incorrect
|
84 |
|
85 | ```ts
|
86 | declare const someUndefinedCondition: boolean | undefined;
|
87 | if (someUndefinedCondition === true) {
|
88 | }
|
89 |
|
90 | declare const someNullCondition: boolean | null;
|
91 | if (someNullCondition !== true) {
|
92 | }
|
93 | ```
|
94 |
|
95 | #### ✅ Correct
|
96 |
|
97 | ```ts
|
98 | declare const someUndefinedCondition: boolean | undefined;
|
99 | if (someUndefinedCondition) {
|
100 | }
|
101 |
|
102 | declare const someNullCondition: boolean | null;
|
103 | if (!someNullCondition) {
|
104 | }
|
105 | ```
|
106 |
|
107 | ### `allowComparingNullableBooleansToFalse`
|
108 |
|
109 | Examples of code for this rule with `{ allowComparingNullableBooleansToFalse: false }`:
|
110 |
|
111 |
|
112 |
|
113 | #### ❌ Incorrect
|
114 |
|
115 | ```ts
|
116 | declare const someUndefinedCondition: boolean | undefined;
|
117 | if (someUndefinedCondition === false) {
|
118 | }
|
119 |
|
120 | declare const someNullCondition: boolean | null;
|
121 | if (someNullCondition !== false) {
|
122 | }
|
123 | ```
|
124 |
|
125 | #### ✅ Correct
|
126 |
|
127 | ```ts
|
128 | declare const someUndefinedCondition: boolean | undefined;
|
129 | if (someUndefinedCondition ?? true) {
|
130 | }
|
131 |
|
132 | declare const someNullCondition: boolean | null;
|
133 | if (!(someNullCondition ?? true)) {
|
134 | }
|
135 | ```
|
136 |
|
137 | ## Fixer
|
138 |
|
139 | | Comparison | Fixer Output | Notes |
|
140 | | :----------------------------: | ------------------------------- | ----------------------------------------------------------------------------------- |
|
141 | | `booleanVar === true` | `booleanVar` | |
|
142 | | `booleanVar !== true` | `!booleanVar` | |
|
143 | | `booleanVar === false` | `!booleanVar` | |
|
144 | | `booleanVar !== false` | `booleanVar` | |
|
145 | | `nullableBooleanVar === true` | `nullableBooleanVar` | Only checked/fixed if the `allowComparingNullableBooleansToTrue` option is `false` |
|
146 | | `nullableBooleanVar !== true` | `!nullableBooleanVar` | Only checked/fixed if the `allowComparingNullableBooleansToTrue` option is `false` |
|
147 | | `nullableBooleanVar === false` | `nullableBooleanVar ?? true` | Only checked/fixed if the `allowComparingNullableBooleansToFalse` option is `false` |
|
148 | | `nullableBooleanVar !== false` | `!(nullableBooleanVar ?? true)` | Only checked/fixed if the `allowComparingNullableBooleansToFalse` option is `false` |
|
149 |
|
150 | ## Related To
|
151 |
|
152 | - TSLint: [no-boolean-literal-compare](https://palantir.github.io/tslint/rules/no-boolean-literal-compare)
|
153 |
|
154 | ## Attributes
|
155 |
|
156 | - Configs:
|
157 | - [ ] ✅ Recommended
|
158 | - [x] 🔒 Strict
|
159 | - [x] 🔧 Fixable
|
160 | - [x] 💭 Requires type information
|