UNPKG

5.37 kBMarkdownView Raw
1# `no-unnecessary-boolean-literal-compare`
2
3Flags unnecessary equality comparisons against boolean literals.
4
5Comparing 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
9This rule ensures that you do not include unnecessary comparisons with boolean literals.
10A comparison is considered unnecessary if it checks a boolean literal against any variable with just the `boolean` type.
11A 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.
14ESLint is not able to distinguish between `false` and `undefined` or `null` values.
15This can cause unintended code changes when using autofix.
16
17**Note**: Throughout this page, only strict equality (`===` and `!==`) are
18used in the examples. However, the implementation of the rule does not
19distinguish between strict and loose equality. Any example below that uses
20`===` would be treated the same way if `==` was used, and any example below
21that uses `!==` would be treated the same way if `!=` was used.
22
23Examples of code for this rule:
24
25<!--tabs-->
26
27### ❌ Incorrect
28
29```ts
30declare const someCondition: boolean;
31if (someCondition === true) {
32}
33```
34
35### ✅ Correct
36
37```ts
38declare const someCondition: boolean;
39if (someCondition) {
40}
41
42declare const someObjectBoolean: boolean | Record<string, unknown>;
43if (someObjectBoolean === true) {
44}
45
46declare const someStringBoolean: boolean | string;
47if (someStringBoolean === true) {
48}
49```
50
51## Options
52
53The rule accepts an options object with the following properties.
54
55```ts
56type 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
66This rule always checks comparisons between a boolean variable and a boolean
67literal. Comparisons between nullable boolean variables and boolean literals
68are **not** checked by default.
69
70```ts
71const defaults = {
72 allowComparingNullableBooleansToTrue: true,
73 allowComparingNullableBooleansToFalse: true,
74};
75```
76
77### `allowComparingNullableBooleansToTrue`
78
79Examples of code for this rule with `{ allowComparingNullableBooleansToTrue: false }`:
80
81<!--tabs-->
82
83#### ❌ Incorrect
84
85```ts
86declare const someUndefinedCondition: boolean | undefined;
87if (someUndefinedCondition === true) {
88}
89
90declare const someNullCondition: boolean | null;
91if (someNullCondition !== true) {
92}
93```
94
95#### ✅ Correct
96
97```ts
98declare const someUndefinedCondition: boolean | undefined;
99if (someUndefinedCondition) {
100}
101
102declare const someNullCondition: boolean | null;
103if (!someNullCondition) {
104}
105```
106
107### `allowComparingNullableBooleansToFalse`
108
109Examples of code for this rule with `{ allowComparingNullableBooleansToFalse: false }`:
110
111<!--tabs-->
112
113#### ❌ Incorrect
114
115```ts
116declare const someUndefinedCondition: boolean | undefined;
117if (someUndefinedCondition === false) {
118}
119
120declare const someNullCondition: boolean | null;
121if (someNullCondition !== false) {
122}
123```
124
125#### ✅ Correct
126
127```ts
128declare const someUndefinedCondition: boolean | undefined;
129if (someUndefinedCondition ?? true) {
130}
131
132declare const someNullCondition: boolean | null;
133if (!(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