@@ -17,6 +17,54 @@ This rule reports when you may consider replacing:
17
17
- An ` || ` operator with ` ?? `
18
18
- An ` ||= ` operator with ` ??= `
19
19
- Ternary expressions (` ?: ` ) that are equivalent to ` || ` or ` ?? ` with ` ?? `
20
+ - Assignment expressions (` = ` ) that can be safely replaced by ` ??= `
21
+
22
+ ## Examples
23
+
24
+ <Tabs >
25
+ <TabItem value = " ❌ Incorrect" >
26
+
27
+ ``` ts
28
+ declare const a: string | null ;
29
+ declare const b: string | null ;
30
+
31
+ const c = a || b ;
32
+
33
+ declare let foo: { a: string } | null ;
34
+ declare function makeFoo(): { a: string };
35
+
36
+ function lazyInitializeFooByTruthiness() {
37
+ if (! foo ) {
38
+ foo = makeFoo ();
39
+ }
40
+ }
41
+
42
+ function lazyInitializeFooByNullCheck() {
43
+ if (foo == null ) {
44
+ foo = makeFoo ();
45
+ }
46
+ }
47
+ ```
48
+
49
+ </TabItem >
50
+ <TabItem value = " ✅ Correct" >
51
+
52
+ ``` ts
53
+ declare const a: string | null ;
54
+ declare const b: string | null ;
55
+
56
+ const c = a ?? b ;
57
+
58
+ declare let foo: { a: string } | null ;
59
+ declare function makeFoo(): { a: string };
60
+
61
+ function lazyInitializeFoo() {
62
+ foo ?? = makeFoo ();
63
+ }
64
+ ```
65
+
66
+ </TabItem >
67
+ </Tabs >
20
68
21
69
:::caution
22
70
This rule will not work as expected if [ ` strictNullChecks ` ] ( https://www.typescriptlang.org/tsconfig#strictNullChecks ) is not enabled.
@@ -84,7 +132,7 @@ Examples of code for this rule with `{ ignoreConditionalTests: false }`:
84
132
<TabItem value = " ❌ Incorrect" >
85
133
86
134
``` ts option='{ "ignoreConditionalTests": false }'
87
- declare const a: string | null ;
135
+ declare let a: string | null ;
88
136
declare const b: string | null ;
89
137
90
138
if (a || b ) {
@@ -102,7 +150,7 @@ a || b ? true : false;
102
150
<TabItem value = " ✅ Correct" >
103
151
104
152
``` ts option='{ "ignoreConditionalTests": false }'
105
- declare const a: string | null ;
153
+ declare let a: string | null ;
106
154
declare const b: string | null ;
107
155
108
156
if (a ?? b ) {
@@ -133,7 +181,7 @@ Examples of code for this rule with `{ ignoreMixedLogicalExpressions: false }`:
133
181
<TabItem value = " ❌ Incorrect" >
134
182
135
183
``` ts option='{ "ignoreMixedLogicalExpressions": false }'
136
- declare const a: string | null ;
184
+ declare let a: string | null ;
137
185
declare const b: string | null ;
138
186
declare const c: string | null ;
139
187
declare const d: string | null ;
@@ -149,7 +197,7 @@ a || (b && c && d);
149
197
<TabItem value = " ✅ Correct" >
150
198
151
199
``` ts option='{ "ignoreMixedLogicalExpressions": false }'
152
- declare const a: string | null ;
200
+ declare let a: string | null ;
153
201
declare const b: string | null ;
154
202
declare const c: string | null ;
155
203
declare const d: string | null ;
0 commit comments