@typescript-eslint/eslint-plugin 8.26.1 → 8.26.2-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -218,26 +218,75 @@ You should be using `strictNullChecks` to ensure complete type-safety in your co
|
|
218
218
|
|
219
219
|
If for some reason you cannot turn on `strictNullChecks`, but still want to use this rule - you can use this option to allow it - but know that the behavior of this rule is _undefined_ with the compiler option turned off. We will not accept bug reports if you are using this option.
|
220
220
|
|
221
|
-
##
|
221
|
+
## Limitations
|
222
222
|
|
223
|
-
|
224
|
-
|
223
|
+
This rule is powered by TypeScript types, therefore, if the types do not match match the runtime behavior, the rule may report inaccurately.
|
224
|
+
This can happen in several commonplace scenarios.
|
225
225
|
|
226
|
-
|
227
|
-
It is due to limitations of TypeScript's type narrowing.
|
228
|
-
See [#9998](https://github.com/microsoft/TypeScript/issues/9998) for details.
|
229
|
-
We recommend using a [type assertion](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#type-assertions) in those cases.
|
226
|
+
### Possibly-undefined indexed access
|
230
227
|
|
231
|
-
|
228
|
+
By default, TypeScript optimistically assumes that indexed access will always return a value.
|
229
|
+
This means that cases like the following will be erroneously flagged as unnecessary conditions:
|
230
|
+
|
231
|
+
```ts showPlaygroundButton
|
232
|
+
const array: string[] = [];
|
233
|
+
const firstElement = array[0];
|
234
|
+
// false positive
|
235
|
+
if (firstElement != null) {
|
236
|
+
// ...
|
237
|
+
}
|
238
|
+
|
239
|
+
const record: Record<string, string> = {};
|
240
|
+
const someValue = record.someKey;
|
241
|
+
// false positive
|
242
|
+
if (someValue != null) {
|
243
|
+
// ...
|
244
|
+
}
|
245
|
+
```
|
246
|
+
|
247
|
+
To get pessimistic, but correct, types for these cases, you can use TypeScript's [`noUncheckedIndexedAccess` compiler option](https://www.typescriptlang.org/tsconfig/#noUncheckedIndexedAccess), though this is often unwieldy in real-world usage.
|
248
|
+
Another workaround is to use `array.at(0)` (which is always possibly-undefined) to indicate array access that may be out-of-bounds.
|
249
|
+
Otherwise, a disable comment will often make sense for these kinds of cases.
|
250
|
+
|
251
|
+
### Values modified within function calls
|
252
|
+
|
253
|
+
The following code will be erroneously flagged as unnecessary, even though the condition is modified within the function call.
|
254
|
+
|
255
|
+
```ts showPlaygroundButton
|
256
|
+
let condition = false;
|
257
|
+
|
258
|
+
const f = () => {
|
259
|
+
condition = Math.random() > 0.5;
|
260
|
+
};
|
261
|
+
f();
|
262
|
+
|
263
|
+
if (condition) {
|
264
|
+
// ...
|
265
|
+
}
|
266
|
+
```
|
267
|
+
|
268
|
+
This occurs due to limitations of TypeScript's type narrowing.
|
269
|
+
See [microsoft/TypeScript#9998](https://github.com/microsoft/TypeScript/issues/9998) for details.
|
270
|
+
We recommend using a [type assertion](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#type-assertions) in these cases, like so:
|
271
|
+
|
272
|
+
```ts showPlaygroundButton
|
232
273
|
let condition = false as boolean;
|
233
274
|
|
234
|
-
const f = () =>
|
275
|
+
const f = () => {
|
276
|
+
condition = Math.random() > 0.5;
|
277
|
+
};
|
235
278
|
f();
|
236
279
|
|
237
280
|
if (condition) {
|
281
|
+
// ...
|
238
282
|
}
|
239
283
|
```
|
240
284
|
|
285
|
+
## When Not To Use It
|
286
|
+
|
287
|
+
If your project is not accurately typed, such as if it's in the process of being converted to TypeScript or is susceptible to [trade-offs in control flow analysis](https://github.com/Microsoft/TypeScript/issues/9998), it may be difficult to enable this rule for particularly non-type-safe areas of code.
|
288
|
+
You might consider using [ESLint disable comments](https://eslint.org/docs/latest/use/configure/rules#using-configuration-comments-1) for those specific situations instead of completely disabling this rule.
|
289
|
+
|
241
290
|
## Related To
|
242
291
|
|
243
292
|
- ESLint: [no-constant-condition](https://eslint.org/docs/rules/no-constant-condition) - `no-unnecessary-condition` is essentially a stronger version of `no-constant-condition`, but requires type information.
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@typescript-eslint/eslint-plugin",
|
3
|
-
"version": "8.26.1",
|
3
|
+
"version": "8.26.2-alpha.1",
|
4
4
|
"description": "TypeScript plugin for ESLint",
|
5
5
|
"files": [
|
6
6
|
"dist",
|
@@ -62,10 +62,10 @@
|
|
62
62
|
},
|
63
63
|
"dependencies": {
|
64
64
|
"@eslint-community/regexpp": "^4.10.0",
|
65
|
-
"@typescript-eslint/scope-manager": "8.26.1",
|
66
|
-
"@typescript-eslint/type-utils": "8.26.1",
|
67
|
-
"@typescript-eslint/utils": "8.26.1",
|
68
|
-
"@typescript-eslint/visitor-keys": "8.26.1",
|
65
|
+
"@typescript-eslint/scope-manager": "8.26.2-alpha.1",
|
66
|
+
"@typescript-eslint/type-utils": "8.26.2-alpha.1",
|
67
|
+
"@typescript-eslint/utils": "8.26.2-alpha.1",
|
68
|
+
"@typescript-eslint/visitor-keys": "8.26.2-alpha.1",
|
69
69
|
"graphemer": "^1.4.0",
|
70
70
|
"ignore": "^5.3.1",
|
71
71
|
"natural-compare": "^1.4.0",
|
@@ -76,8 +76,8 @@
|
|
76
76
|
"@types/marked": "^5.0.2",
|
77
77
|
"@types/mdast": "^4.0.3",
|
78
78
|
"@types/natural-compare": "*",
|
79
|
-
"@typescript-eslint/rule-schema-to-typescript-types": "8.26.1",
|
80
|
-
"@typescript-eslint/rule-tester": "8.26.1",
|
79
|
+
"@typescript-eslint/rule-schema-to-typescript-types": "8.26.2-alpha.1",
|
80
|
+
"@typescript-eslint/rule-tester": "8.26.2-alpha.1",
|
81
81
|
"ajv": "^6.12.6",
|
82
82
|
"cross-env": "^7.0.3",
|
83
83
|
"cross-fetch": "*",
|