@typescript-eslint/eslint-plugin 8.33.1-alpha.5 → 8.33.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.
- package/package.json +9 -10
- package/docs/rules/README.md +0 -57
- package/docs/rules/TEMPLATE.md +0 -36
- package/docs/rules/adjacent-overload-signatures.mdx +0 -105
- package/docs/rules/array-type.mdx +0 -126
- package/docs/rules/await-thenable.mdx +0 -184
- package/docs/rules/ban-ts-comment.mdx +0 -165
- package/docs/rules/ban-tslint-comment.mdx +0 -45
- package/docs/rules/ban-types.md +0 -26
- package/docs/rules/block-spacing.md +0 -15
- package/docs/rules/brace-style.md +0 -15
- package/docs/rules/camelcase.md +0 -15
- package/docs/rules/class-literal-property-style.mdx +0 -112
- package/docs/rules/class-methods-use-this.mdx +0 -135
- package/docs/rules/comma-dangle.md +0 -15
- package/docs/rules/comma-spacing.md +0 -15
- package/docs/rules/consistent-generic-constructors.mdx +0 -87
- package/docs/rules/consistent-indexed-object-style.mdx +0 -105
- package/docs/rules/consistent-return.mdx +0 -51
- package/docs/rules/consistent-type-assertions.mdx +0 -196
- package/docs/rules/consistent-type-definitions.mdx +0 -133
- package/docs/rules/consistent-type-exports.mdx +0 -97
- package/docs/rules/consistent-type-imports.mdx +0 -139
- package/docs/rules/default-param-last.mdx +0 -59
- package/docs/rules/dot-notation.mdx +0 -94
- package/docs/rules/explicit-function-return-type.mdx +0 -359
- package/docs/rules/explicit-member-accessibility.mdx +0 -353
- package/docs/rules/explicit-module-boundary-types.mdx +0 -287
- package/docs/rules/func-call-spacing.md +0 -15
- package/docs/rules/indent.md +0 -15
- package/docs/rules/init-declarations.mdx +0 -12
- package/docs/rules/key-spacing.md +0 -15
- package/docs/rules/keyword-spacing.md +0 -15
- package/docs/rules/lines-around-comment.md +0 -15
- package/docs/rules/lines-between-class-members.md +0 -15
- package/docs/rules/max-params.mdx +0 -54
- package/docs/rules/member-delimiter-style.md +0 -15
- package/docs/rules/member-ordering.mdx +0 -1483
- package/docs/rules/method-signature-style.mdx +0 -124
- package/docs/rules/naming-convention.mdx +0 -755
- package/docs/rules/no-array-constructor.mdx +0 -34
- package/docs/rules/no-array-delete.mdx +0 -44
- package/docs/rules/no-base-to-string.mdx +0 -115
- package/docs/rules/no-confusing-non-null-assertion.mdx +0 -75
- package/docs/rules/no-confusing-void-expression.mdx +0 -148
- package/docs/rules/no-deprecated.mdx +0 -119
- package/docs/rules/no-dupe-class-members.mdx +0 -16
- package/docs/rules/no-duplicate-enum-values.mdx +0 -66
- package/docs/rules/no-duplicate-imports.mdx +0 -17
- package/docs/rules/no-duplicate-type-constituents.mdx +0 -89
- package/docs/rules/no-dynamic-delete.mdx +0 -64
- package/docs/rules/no-empty-function.mdx +0 -94
- package/docs/rules/no-empty-interface.mdx +0 -75
- package/docs/rules/no-empty-object-type.mdx +0 -150
- package/docs/rules/no-explicit-any.mdx +0 -177
- package/docs/rules/no-extra-non-null-assertion.mdx +0 -60
- package/docs/rules/no-extra-parens.md +0 -15
- package/docs/rules/no-extra-semi.md +0 -15
- package/docs/rules/no-extraneous-class.mdx +0 -329
- package/docs/rules/no-floating-promises.mdx +0 -282
- package/docs/rules/no-for-in-array.mdx +0 -67
- package/docs/rules/no-implied-eval.mdx +0 -106
- package/docs/rules/no-import-type-side-effects.mdx +0 -80
- package/docs/rules/no-inferrable-types.mdx +0 -113
- package/docs/rules/no-invalid-this.mdx +0 -16
- package/docs/rules/no-invalid-void-type.mdx +0 -119
- package/docs/rules/no-loop-func.mdx +0 -12
- package/docs/rules/no-loss-of-precision.mdx +0 -17
- package/docs/rules/no-magic-numbers.mdx +0 -131
- package/docs/rules/no-meaningless-void-operator.mdx +0 -61
- package/docs/rules/no-misused-new.mdx +0 -53
- package/docs/rules/no-misused-promises.mdx +0 -314
- package/docs/rules/no-misused-spread.mdx +0 -132
- package/docs/rules/no-mixed-enums.mdx +0 -96
- package/docs/rules/no-namespace.mdx +0 -157
- package/docs/rules/no-non-null-asserted-nullish-coalescing.mdx +0 -60
- package/docs/rules/no-non-null-asserted-optional-chain.mdx +0 -46
- package/docs/rules/no-non-null-assertion.mdx +0 -48
- package/docs/rules/no-parameter-properties.mdx +0 -16
- package/docs/rules/no-redeclare.mdx +0 -79
- package/docs/rules/no-redundant-type-constituents.mdx +0 -102
- package/docs/rules/no-require-imports.mdx +0 -114
- package/docs/rules/no-restricted-imports.mdx +0 -84
- package/docs/rules/no-restricted-types.mdx +0 -70
- package/docs/rules/no-shadow.mdx +0 -143
- package/docs/rules/no-this-alias.mdx +0 -124
- package/docs/rules/no-type-alias.mdx +0 -626
- package/docs/rules/no-unnecessary-boolean-literal-compare.mdx +0 -165
- package/docs/rules/no-unnecessary-condition.mdx +0 -293
- package/docs/rules/no-unnecessary-parameter-property-assignment.mdx +0 -42
- package/docs/rules/no-unnecessary-qualifier.mdx +0 -57
- package/docs/rules/no-unnecessary-template-expression.mdx +0 -108
- package/docs/rules/no-unnecessary-type-arguments.mdx +0 -85
- package/docs/rules/no-unnecessary-type-assertion.mdx +0 -97
- package/docs/rules/no-unnecessary-type-constraint.mdx +0 -61
- package/docs/rules/no-unnecessary-type-conversion.mdx +0 -79
- package/docs/rules/no-unnecessary-type-parameters.mdx +0 -255
- package/docs/rules/no-unsafe-argument.mdx +0 -98
- package/docs/rules/no-unsafe-assignment.mdx +0 -101
- package/docs/rules/no-unsafe-call.mdx +0 -120
- package/docs/rules/no-unsafe-declaration-merging.mdx +0 -65
- package/docs/rules/no-unsafe-enum-comparison.mdx +0 -98
- package/docs/rules/no-unsafe-function-type.mdx +0 -65
- package/docs/rules/no-unsafe-member-access.mdx +0 -81
- package/docs/rules/no-unsafe-return.mdx +0 -126
- package/docs/rules/no-unsafe-type-assertion.mdx +0 -63
- package/docs/rules/no-unsafe-unary-minus.mdx +0 -60
- package/docs/rules/no-unused-expressions.mdx +0 -52
- package/docs/rules/no-unused-vars.mdx +0 -136
- package/docs/rules/no-use-before-define.mdx +0 -98
- package/docs/rules/no-useless-constructor.mdx +0 -21
- package/docs/rules/no-useless-empty-export.mdx +0 -53
- package/docs/rules/no-useless-template-literals.mdx +0 -9
- package/docs/rules/no-var-requires.mdx +0 -77
- package/docs/rules/no-wrapper-object-types.mdx +0 -75
- package/docs/rules/non-nullable-type-assertion-style.mdx +0 -47
- package/docs/rules/object-curly-spacing.md +0 -15
- package/docs/rules/only-throw-error.mdx +0 -150
- package/docs/rules/padding-line-between-statements.md +0 -15
- package/docs/rules/parameter-properties.mdx +0 -522
- package/docs/rules/prefer-as-const.mdx +0 -51
- package/docs/rules/prefer-destructuring.mdx +0 -101
- package/docs/rules/prefer-enum-initializers.mdx +0 -68
- package/docs/rules/prefer-find.mdx +0 -45
- package/docs/rules/prefer-for-of.mdx +0 -50
- package/docs/rules/prefer-function-type.mdx +0 -98
- package/docs/rules/prefer-includes.mdx +0 -81
- package/docs/rules/prefer-literal-enum-member.mdx +0 -111
- package/docs/rules/prefer-namespace-keyword.mdx +0 -51
- package/docs/rules/prefer-nullish-coalescing.mdx +0 -349
- package/docs/rules/prefer-optional-chain.mdx +0 -304
- package/docs/rules/prefer-promise-reject-errors.mdx +0 -78
- package/docs/rules/prefer-readonly-parameter-types.mdx +0 -408
- package/docs/rules/prefer-readonly.mdx +0 -111
- package/docs/rules/prefer-reduce-type-parameter.mdx +0 -66
- package/docs/rules/prefer-regexp-exec.mdx +0 -52
- package/docs/rules/prefer-return-this-type.mdx +0 -93
- package/docs/rules/prefer-string-starts-ends-with.mdx +0 -84
- package/docs/rules/prefer-ts-expect-error.mdx +0 -86
- package/docs/rules/promise-function-async.mdx +0 -143
- package/docs/rules/quotes.md +0 -15
- package/docs/rules/related-getter-setter-pairs.mdx +0 -61
- package/docs/rules/require-array-sort-compare.mdx +0 -89
- package/docs/rules/require-await.mdx +0 -53
- package/docs/rules/restrict-plus-operands.mdx +0 -245
- package/docs/rules/restrict-template-expressions.mdx +0 -167
- package/docs/rules/return-await.mdx +0 -339
- package/docs/rules/semi.md +0 -15
- package/docs/rules/sort-type-constituents.mdx +0 -209
- package/docs/rules/sort-type-union-intersection-members.mdx +0 -16
- package/docs/rules/space-before-blocks.md +0 -15
- package/docs/rules/space-before-function-paren.md +0 -15
- package/docs/rules/space-infix-ops.md +0 -15
- package/docs/rules/strict-boolean-expressions.mdx +0 -184
- package/docs/rules/switch-exhaustiveness-check.mdx +0 -280
- package/docs/rules/triple-slash-reference.mdx +0 -129
- package/docs/rules/type-annotation-spacing.md +0 -15
- package/docs/rules/typedef.mdx +0 -350
- package/docs/rules/unbound-method.mdx +0 -114
- package/docs/rules/unified-signatures.mdx +0 -132
- package/docs/rules/use-unknown-in-catch-callback-variable.mdx +0 -97
@@ -1,165 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Disallow unnecessary equality comparisons against boolean literals.'
|
3
|
-
---
|
4
|
-
|
5
|
-
import Tabs from '@theme/Tabs';
|
6
|
-
import TabItem from '@theme/TabItem';
|
7
|
-
|
8
|
-
> 🛑 This file is source code, not the primary documentation location! 🛑
|
9
|
-
>
|
10
|
-
> See **https://typescript-eslint.io/rules/no-unnecessary-boolean-literal-compare** for documentation.
|
11
|
-
|
12
|
-
Comparing boolean values to boolean literals is unnecessary: those comparisons result in the same booleans.
|
13
|
-
Using the boolean values directly, or via a unary negation (`!value`), is more concise and clearer.
|
14
|
-
|
15
|
-
This rule ensures that you do not include unnecessary comparisons with boolean literals.
|
16
|
-
A comparison is considered unnecessary if it checks a boolean literal against any variable with just the `boolean` type.
|
17
|
-
A comparison is **_not_** considered unnecessary if the type is a union of booleans (`string | boolean`, `SomeObject | boolean`, etc.).
|
18
|
-
|
19
|
-
## Examples
|
20
|
-
|
21
|
-
:::note
|
22
|
-
Throughout this page, only strict equality (`===` and `!==`) are used in the examples.
|
23
|
-
However, the implementation of the rule does not distinguish between strict and loose equality.
|
24
|
-
Any example below that uses `===` would be treated the same way if `==` was used, and `!==` would be treated the same way if `!=` was used.
|
25
|
-
:::
|
26
|
-
|
27
|
-
<Tabs>
|
28
|
-
<TabItem value="❌ Incorrect">
|
29
|
-
|
30
|
-
```ts
|
31
|
-
declare const someCondition: boolean;
|
32
|
-
if (someCondition === true) {
|
33
|
-
}
|
34
|
-
```
|
35
|
-
|
36
|
-
</TabItem>
|
37
|
-
<TabItem value="✅ Correct">
|
38
|
-
|
39
|
-
```ts
|
40
|
-
declare const someCondition: boolean;
|
41
|
-
if (someCondition) {
|
42
|
-
}
|
43
|
-
|
44
|
-
declare const someObjectBoolean: boolean | Record<string, unknown>;
|
45
|
-
if (someObjectBoolean === true) {
|
46
|
-
}
|
47
|
-
|
48
|
-
declare const someStringBoolean: boolean | string;
|
49
|
-
if (someStringBoolean === true) {
|
50
|
-
}
|
51
|
-
```
|
52
|
-
|
53
|
-
</TabItem>
|
54
|
-
</Tabs>
|
55
|
-
|
56
|
-
## Options
|
57
|
-
|
58
|
-
This rule always checks comparisons between a boolean variable and a boolean
|
59
|
-
literal. Comparisons between nullable boolean variables and boolean literals
|
60
|
-
are **not** checked by default.
|
61
|
-
|
62
|
-
### `allowComparingNullableBooleansToTrue`
|
63
|
-
|
64
|
-
{/* insert option description */}
|
65
|
-
|
66
|
-
Examples of code for this rule with `{ allowComparingNullableBooleansToTrue: false }`:
|
67
|
-
|
68
|
-
<Tabs>
|
69
|
-
<TabItem value="❌ Incorrect">
|
70
|
-
|
71
|
-
```ts option='{ "allowComparingNullableBooleansToTrue": false }'
|
72
|
-
declare const someUndefinedCondition: boolean | undefined;
|
73
|
-
if (someUndefinedCondition === true) {
|
74
|
-
}
|
75
|
-
|
76
|
-
declare const someNullCondition: boolean | null;
|
77
|
-
if (someNullCondition !== true) {
|
78
|
-
}
|
79
|
-
```
|
80
|
-
|
81
|
-
</TabItem>
|
82
|
-
<TabItem value="✅ Correct">
|
83
|
-
|
84
|
-
```ts option='{ "allowComparingNullableBooleansToTrue": false }'
|
85
|
-
declare const someUndefinedCondition: boolean | undefined;
|
86
|
-
if (someUndefinedCondition) {
|
87
|
-
}
|
88
|
-
|
89
|
-
declare const someNullCondition: boolean | null;
|
90
|
-
if (!someNullCondition) {
|
91
|
-
}
|
92
|
-
```
|
93
|
-
|
94
|
-
</TabItem>
|
95
|
-
</Tabs>
|
96
|
-
|
97
|
-
### `allowComparingNullableBooleansToFalse`
|
98
|
-
|
99
|
-
{/* insert option description */}
|
100
|
-
|
101
|
-
Examples of code for this rule with `{ allowComparingNullableBooleansToFalse: false }`:
|
102
|
-
|
103
|
-
<Tabs>
|
104
|
-
<TabItem value="❌ Incorrect">
|
105
|
-
|
106
|
-
```ts option='{ "allowComparingNullableBooleansToFalse": false }'
|
107
|
-
declare const someUndefinedCondition: boolean | undefined;
|
108
|
-
if (someUndefinedCondition === false) {
|
109
|
-
}
|
110
|
-
|
111
|
-
declare const someNullCondition: boolean | null;
|
112
|
-
if (someNullCondition !== false) {
|
113
|
-
}
|
114
|
-
```
|
115
|
-
|
116
|
-
</TabItem>
|
117
|
-
<TabItem value="✅ Correct">
|
118
|
-
|
119
|
-
```ts option='{ "allowComparingNullableBooleansToFalse": false }'
|
120
|
-
declare const someUndefinedCondition: boolean | undefined;
|
121
|
-
if (!(someUndefinedCondition ?? true)) {
|
122
|
-
}
|
123
|
-
|
124
|
-
declare const someNullCondition: boolean | null;
|
125
|
-
if (someNullCondition ?? true) {
|
126
|
-
}
|
127
|
-
```
|
128
|
-
|
129
|
-
</TabItem>
|
130
|
-
</Tabs>
|
131
|
-
|
132
|
-
### `allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing`
|
133
|
-
|
134
|
-
:::danger Deprecated
|
135
|
-
|
136
|
-
This option will be removed in the next major version of typescript-eslint.
|
137
|
-
|
138
|
-
:::
|
139
|
-
|
140
|
-
{/* insert option description */}
|
141
|
-
|
142
|
-
Without `strictNullChecks`, TypeScript essentially erases `undefined` and `null` from the types. This means when this rule inspects the types from a variable, **it will not be able to tell that the variable might be `null` or `undefined`**, which essentially makes this rule useless.
|
143
|
-
|
144
|
-
You should be using `strictNullChecks` to ensure complete type-safety in your codebase.
|
145
|
-
|
146
|
-
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.
|
147
|
-
|
148
|
-
## Fixer
|
149
|
-
|
150
|
-
| Comparison | Fixer Output | Notes |
|
151
|
-
| :----------------------------: | ------------------------------- | ----------------------------------------------------------------------------------- |
|
152
|
-
| `booleanVar === true` | `booleanVar` | |
|
153
|
-
| `booleanVar !== true` | `!booleanVar` | |
|
154
|
-
| `booleanVar === false` | `!booleanVar` | |
|
155
|
-
| `booleanVar !== false` | `booleanVar` | |
|
156
|
-
| `nullableBooleanVar === true` | `nullableBooleanVar` | Only checked/fixed if the `allowComparingNullableBooleansToTrue` option is `false` |
|
157
|
-
| `nullableBooleanVar !== true` | `!nullableBooleanVar` | Only checked/fixed if the `allowComparingNullableBooleansToTrue` option is `false` |
|
158
|
-
| `nullableBooleanVar === false` | `!(nullableBooleanVar ?? true)` | Only checked/fixed if the `allowComparingNullableBooleansToFalse` option is `false` |
|
159
|
-
| `nullableBooleanVar !== false` | `nullableBooleanVar ?? true` | Only checked/fixed if the `allowComparingNullableBooleansToFalse` option is `false` |
|
160
|
-
|
161
|
-
## When Not To Use It
|
162
|
-
|
163
|
-
Do not use this rule when `strictNullChecks` is disabled.
|
164
|
-
ESLint is not able to distinguish between `false` and `undefined` or `null` values.
|
165
|
-
This can cause unintended code changes when using autofix.
|
@@ -1,293 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Disallow conditionals where the type is always truthy or always falsy.'
|
3
|
-
---
|
4
|
-
|
5
|
-
import Tabs from '@theme/Tabs';
|
6
|
-
import TabItem from '@theme/TabItem';
|
7
|
-
|
8
|
-
> 🛑 This file is source code, not the primary documentation location! 🛑
|
9
|
-
>
|
10
|
-
> See **https://typescript-eslint.io/rules/no-unnecessary-condition** for documentation.
|
11
|
-
|
12
|
-
Any expression being used as a condition must be able to evaluate as truthy or falsy in order to be considered "necessary".
|
13
|
-
Conversely, any expression that always evaluates to truthy or always evaluates to falsy, as determined by the type of the expression, is considered unnecessary and will be flagged by this rule.
|
14
|
-
|
15
|
-
The following expressions are checked:
|
16
|
-
|
17
|
-
- Arguments to the `&&`, `||` and `?:` (ternary) operators
|
18
|
-
- Conditions for `if`, `for`, `while`, and `do-while` statements
|
19
|
-
- `case`s in `switch` statements
|
20
|
-
- Base values of optional chain expressions
|
21
|
-
|
22
|
-
## Examples
|
23
|
-
|
24
|
-
<Tabs>
|
25
|
-
<TabItem value="❌ Incorrect">
|
26
|
-
|
27
|
-
```ts
|
28
|
-
function head<T>(items: T[]) {
|
29
|
-
// items can never be nullable, so this is unnecessary
|
30
|
-
if (items) {
|
31
|
-
return items[0].toUpperCase();
|
32
|
-
}
|
33
|
-
}
|
34
|
-
|
35
|
-
function foo(arg: 'bar' | 'baz') {
|
36
|
-
// arg is never nullable or empty string, so this is unnecessary
|
37
|
-
if (arg) {
|
38
|
-
}
|
39
|
-
}
|
40
|
-
|
41
|
-
function bar<T>(arg: string) {
|
42
|
-
// arg can never be nullish, so ?. is unnecessary
|
43
|
-
return arg?.length;
|
44
|
-
}
|
45
|
-
|
46
|
-
// Checks array predicate return types, where possible
|
47
|
-
[
|
48
|
-
[1, 2],
|
49
|
-
[3, 4],
|
50
|
-
].filter(t => t); // number[] is always truthy
|
51
|
-
```
|
52
|
-
|
53
|
-
</TabItem>
|
54
|
-
<TabItem value="✅ Correct">
|
55
|
-
|
56
|
-
```ts
|
57
|
-
function head<T>(items: T[]) {
|
58
|
-
// Necessary, since items.length might be 0
|
59
|
-
if (items.length) {
|
60
|
-
return items[0].toUpperCase();
|
61
|
-
}
|
62
|
-
}
|
63
|
-
|
64
|
-
function foo(arg: string) {
|
65
|
-
// Necessary, since arg might be ''.
|
66
|
-
if (arg) {
|
67
|
-
}
|
68
|
-
}
|
69
|
-
|
70
|
-
function bar(arg?: string | null) {
|
71
|
-
// Necessary, since arg might be nullish
|
72
|
-
return arg?.length;
|
73
|
-
}
|
74
|
-
|
75
|
-
[0, 1, 2, 3].filter(t => t); // number can be truthy or falsy
|
76
|
-
```
|
77
|
-
|
78
|
-
</TabItem>
|
79
|
-
</Tabs>
|
80
|
-
|
81
|
-
## Options
|
82
|
-
|
83
|
-
### `allowConstantLoopConditions`
|
84
|
-
|
85
|
-
{/* insert option description */}
|
86
|
-
|
87
|
-
#### `'never'`
|
88
|
-
|
89
|
-
Disallow constant conditions in loops. Same as `false`.
|
90
|
-
|
91
|
-
Example of incorrect code for `{ allowConstantLoopConditions: 'never' }`:
|
92
|
-
|
93
|
-
```ts option='{ "allowConstantLoopConditions": "never" }' showPlaygroundButton
|
94
|
-
while (true) {
|
95
|
-
// ...
|
96
|
-
}
|
97
|
-
|
98
|
-
for (; true; ) {
|
99
|
-
// ...
|
100
|
-
}
|
101
|
-
|
102
|
-
do {
|
103
|
-
// ...
|
104
|
-
} while (true);
|
105
|
-
```
|
106
|
-
|
107
|
-
#### `'always'`
|
108
|
-
|
109
|
-
Allow constant conditions in loops. Same as `true`.
|
110
|
-
|
111
|
-
Example of correct code for `{ allowConstantLoopConditions: 'always' }`:
|
112
|
-
|
113
|
-
```ts option='{ "allowConstantLoopConditions": "always" }' showPlaygroundButton
|
114
|
-
while (true) {
|
115
|
-
// ...
|
116
|
-
}
|
117
|
-
|
118
|
-
for (; true; ) {
|
119
|
-
// ...
|
120
|
-
}
|
121
|
-
|
122
|
-
do {
|
123
|
-
// ...
|
124
|
-
} while (true);
|
125
|
-
```
|
126
|
-
|
127
|
-
#### `'only-allowed-literals'`
|
128
|
-
|
129
|
-
Permit idiomatic constant literal conditions in `while` loop conditions.
|
130
|
-
|
131
|
-
Specifically, `true`, `false`, `0`, and `1` are allowed despite always being truthy or falsy, as these are common patterns and are not likely to represent developer errors.
|
132
|
-
|
133
|
-
Example of correct code for `{ allowConstantLoopConditions: 'only-allowed-literals' }`:
|
134
|
-
|
135
|
-
```ts option='{ "allowConstantLoopConditions": "only-allowed-literals" }' showPlaygroundButton
|
136
|
-
while (true) {
|
137
|
-
// ...
|
138
|
-
}
|
139
|
-
```
|
140
|
-
|
141
|
-
Example of incorrect code for `{ allowConstantLoopConditions: 'only-allowed-literals' }`:
|
142
|
-
|
143
|
-
```ts option='{ "allowConstantLoopConditions": "only-allowed-literals" }' showPlaygroundButton
|
144
|
-
// `alwaysTrue` has the type of `true` (which isn't allowed)
|
145
|
-
// as only the literal value of `true` is allowed.
|
146
|
-
|
147
|
-
declare const alwaysTrue: true;
|
148
|
-
|
149
|
-
while (alwaysTrue) {
|
150
|
-
// ...
|
151
|
-
}
|
152
|
-
|
153
|
-
// not even a variable that references the value of `true` is allowed, only
|
154
|
-
// the literal value of `true` used directly.
|
155
|
-
|
156
|
-
const thisIsTrue = true;
|
157
|
-
|
158
|
-
while (thisIsTrue) {
|
159
|
-
// ...
|
160
|
-
}
|
161
|
-
```
|
162
|
-
|
163
|
-
### `checkTypePredicates`
|
164
|
-
|
165
|
-
{/* insert option description */}
|
166
|
-
|
167
|
-
Example of additional incorrect code with `{ checkTypePredicates: true }`:
|
168
|
-
|
169
|
-
```ts option='{ "checkTypePredicates": true }' showPlaygroundButton
|
170
|
-
function assert(condition: unknown): asserts condition {
|
171
|
-
if (!condition) {
|
172
|
-
throw new Error('Condition is falsy');
|
173
|
-
}
|
174
|
-
}
|
175
|
-
|
176
|
-
assert(false); // Unnecessary; condition is always falsy.
|
177
|
-
|
178
|
-
const neverNull = {};
|
179
|
-
assert(neverNull); // Unnecessary; condition is always truthy.
|
180
|
-
|
181
|
-
function isString(value: unknown): value is string {
|
182
|
-
return typeof value === 'string';
|
183
|
-
}
|
184
|
-
|
185
|
-
declare const s: string;
|
186
|
-
|
187
|
-
// Unnecessary; s is always a string.
|
188
|
-
if (isString(s)) {
|
189
|
-
}
|
190
|
-
|
191
|
-
function assertIsString(value: unknown): asserts value is string {
|
192
|
-
if (!isString(value)) {
|
193
|
-
throw new Error('Value is not a string');
|
194
|
-
}
|
195
|
-
}
|
196
|
-
|
197
|
-
assertIsString(s); // Unnecessary; s is always a string.
|
198
|
-
```
|
199
|
-
|
200
|
-
Whether this option makes sense for your project may vary.
|
201
|
-
Some projects may intentionally use type predicates to ensure that runtime values do indeed match the types according to TypeScript, especially in test code.
|
202
|
-
Often, it makes sense to use eslint-disable comments in these cases, with a comment indicating why the condition should be checked at runtime, despite appearing unnecessary.
|
203
|
-
However, in some contexts, it may be more appropriate to keep this option disabled entirely.
|
204
|
-
|
205
|
-
### `allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing`
|
206
|
-
|
207
|
-
{/* insert option description */}
|
208
|
-
|
209
|
-
:::danger Deprecated
|
210
|
-
This option will be removed in the next major version of typescript-eslint.
|
211
|
-
:::
|
212
|
-
|
213
|
-
If this is set to `false`, then the rule will error on every file whose `tsconfig.json` does _not_ have the `strictNullChecks` compiler option (or `strict`) set to `true`.
|
214
|
-
|
215
|
-
Without `strictNullChecks`, TypeScript essentially erases `undefined` and `null` from the types. This means when this rule inspects the types from a variable, **it will not be able to tell that the variable might be `null` or `undefined`**, which essentially makes this rule useless.
|
216
|
-
|
217
|
-
You should be using `strictNullChecks` to ensure complete type-safety in your codebase.
|
218
|
-
|
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
|
-
|
221
|
-
## Limitations
|
222
|
-
|
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
|
-
|
226
|
-
### Possibly-undefined indexed access
|
227
|
-
|
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
|
273
|
-
let condition = false as boolean;
|
274
|
-
|
275
|
-
const f = () => {
|
276
|
-
condition = Math.random() > 0.5;
|
277
|
-
};
|
278
|
-
f();
|
279
|
-
|
280
|
-
if (condition) {
|
281
|
-
// ...
|
282
|
-
}
|
283
|
-
```
|
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
|
-
|
290
|
-
## Related To
|
291
|
-
|
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.
|
293
|
-
- [strict-boolean-expressions](./strict-boolean-expressions.mdx) - a more opinionated version of `no-unnecessary-condition`. `strict-boolean-expressions` enforces a specific code style, while `no-unnecessary-condition` is about correctness.
|
@@ -1,42 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Disallow unnecessary assignment of constructor property parameter.'
|
3
|
-
---
|
4
|
-
|
5
|
-
import Tabs from '@theme/Tabs';
|
6
|
-
import TabItem from '@theme/TabItem';
|
7
|
-
|
8
|
-
> 🛑 This file is source code, not the primary documentation location! 🛑
|
9
|
-
>
|
10
|
-
> See **https://typescript-eslint.io/rules/no-unnecessary-parameter-property-assignment** for documentation.
|
11
|
-
|
12
|
-
[TypeScript's parameter properties](https://www.typescriptlang.org/docs/handbook/2/classes.html#parameter-properties) allow creating and initializing a member in one place.
|
13
|
-
Therefore, in most cases, it is not necessary to assign parameter properties of the same name to members within a constructor.
|
14
|
-
|
15
|
-
## Examples
|
16
|
-
|
17
|
-
<Tabs>
|
18
|
-
<TabItem value="❌ Incorrect">
|
19
|
-
|
20
|
-
```ts
|
21
|
-
class Foo {
|
22
|
-
constructor(public bar: string) {
|
23
|
-
this.bar = bar;
|
24
|
-
}
|
25
|
-
}
|
26
|
-
```
|
27
|
-
|
28
|
-
</TabItem>
|
29
|
-
<TabItem value="✅ Correct">
|
30
|
-
|
31
|
-
```ts
|
32
|
-
class Foo {
|
33
|
-
constructor(public bar: string) {}
|
34
|
-
}
|
35
|
-
```
|
36
|
-
|
37
|
-
</TabItem>
|
38
|
-
</Tabs>
|
39
|
-
|
40
|
-
## When Not To Use It
|
41
|
-
|
42
|
-
If you don't use parameter properties, you can ignore this rule.
|
@@ -1,57 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Disallow unnecessary namespace qualifiers.'
|
3
|
-
---
|
4
|
-
|
5
|
-
import Tabs from '@theme/Tabs';
|
6
|
-
import TabItem from '@theme/TabItem';
|
7
|
-
|
8
|
-
> 🛑 This file is source code, not the primary documentation location! 🛑
|
9
|
-
>
|
10
|
-
> See **https://typescript-eslint.io/rules/no-unnecessary-qualifier** for documentation.
|
11
|
-
|
12
|
-
Members of TypeScript enums and namespaces are generally retrieved as qualified property lookups: e.g. `Enum.member`.
|
13
|
-
However, when accessed within their parent enum or namespace, the qualifier is unnecessary: e.g. just `member` instead of `Enum.member`.
|
14
|
-
This rule reports when an enum or namespace qualifier is unnecessary.
|
15
|
-
|
16
|
-
## Examples
|
17
|
-
|
18
|
-
<Tabs>
|
19
|
-
<TabItem value="❌ Incorrect">
|
20
|
-
|
21
|
-
```ts
|
22
|
-
enum A {
|
23
|
-
B,
|
24
|
-
C = A.B,
|
25
|
-
}
|
26
|
-
```
|
27
|
-
|
28
|
-
```ts
|
29
|
-
namespace A {
|
30
|
-
export type B = number;
|
31
|
-
const x: A.B = 3;
|
32
|
-
}
|
33
|
-
```
|
34
|
-
|
35
|
-
</TabItem>
|
36
|
-
<TabItem value="✅ Correct">
|
37
|
-
|
38
|
-
```ts
|
39
|
-
enum A {
|
40
|
-
B,
|
41
|
-
C = B,
|
42
|
-
}
|
43
|
-
```
|
44
|
-
|
45
|
-
```ts
|
46
|
-
namespace A {
|
47
|
-
export type B = number;
|
48
|
-
const x: B = 3;
|
49
|
-
}
|
50
|
-
```
|
51
|
-
|
52
|
-
</TabItem>
|
53
|
-
</Tabs>
|
54
|
-
|
55
|
-
## When Not To Use It
|
56
|
-
|
57
|
-
If you explicitly prefer to use fully qualified names, such as for explicitness, then you don't need to use this rule.
|
@@ -1,108 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Disallow unnecessary template expressions.'
|
3
|
-
---
|
4
|
-
|
5
|
-
import Tabs from '@theme/Tabs';
|
6
|
-
import TabItem from '@theme/TabItem';
|
7
|
-
|
8
|
-
> 🛑 This file is source code, not the primary documentation location! 🛑
|
9
|
-
>
|
10
|
-
> See **https://typescript-eslint.io/rules/no-unnecessary-template-expression** for documentation.
|
11
|
-
|
12
|
-
This rule reports template literals that contain substitution expressions (also variously referred to as embedded expressions or string interpolations) that are unnecessary and can be simplified.
|
13
|
-
|
14
|
-
:::info[Migration from `no-useless-template-literals`]
|
15
|
-
|
16
|
-
This rule was formerly known as [`no-useless-template-literals`](./no-useless-template-literals.mdx).
|
17
|
-
The new name is a drop-in replacement with identical functionality.
|
18
|
-
|
19
|
-
:::
|
20
|
-
|
21
|
-
## Examples
|
22
|
-
|
23
|
-
<Tabs>
|
24
|
-
<TabItem value="❌ Incorrect">
|
25
|
-
|
26
|
-
```ts
|
27
|
-
// Static values can be incorporated into the surrounding template.
|
28
|
-
|
29
|
-
const ab1 = `${'a'}${'b'}`;
|
30
|
-
const ab2 = `a${'b'}`;
|
31
|
-
type AB1 = `${'A'}${'B'}`;
|
32
|
-
type AB2 = `A${'B'}`;
|
33
|
-
|
34
|
-
const stringWithNumber = `${'1 + 1 = '}${2}`;
|
35
|
-
|
36
|
-
const stringWithBoolean = `${'true is '}${true}`;
|
37
|
-
|
38
|
-
// Some simple expressions that are already strings
|
39
|
-
// can be rewritten without a template at all.
|
40
|
-
|
41
|
-
const text = 'a';
|
42
|
-
const wrappedText = `${text}`;
|
43
|
-
type Text = 'A';
|
44
|
-
type WrappedText = `${Text}`;
|
45
|
-
|
46
|
-
declare const intersectionWithString: string & { _brand: 'test-brand' };
|
47
|
-
const wrappedIntersection = `${intersectionWithString}`;
|
48
|
-
type IntersectionWithString = string & { _brand: 'test-brand' };
|
49
|
-
type WrappedIntersection = `${IntersectionWithString}`;
|
50
|
-
```
|
51
|
-
|
52
|
-
</TabItem>
|
53
|
-
<TabItem value="✅ Correct">
|
54
|
-
|
55
|
-
```ts
|
56
|
-
// Static values can be incorporated into the surrounding template.
|
57
|
-
|
58
|
-
const ab1 = `ab`;
|
59
|
-
const ab2 = `ab`;
|
60
|
-
type AB = `AB`;
|
61
|
-
|
62
|
-
// Transforming enum members into string unions using template literals is allowed.
|
63
|
-
enum ABC {
|
64
|
-
A = 'A',
|
65
|
-
B = 'B',
|
66
|
-
C = 'C',
|
67
|
-
}
|
68
|
-
type ABCUnion = `${ABC}`;
|
69
|
-
type A = `${ABC.A}`;
|
70
|
-
|
71
|
-
// Interpolating type parameters is allowed.
|
72
|
-
type TextUtil<T extends string> = `${T}`;
|
73
|
-
|
74
|
-
const stringWithNumber = `1 + 1 = 2`;
|
75
|
-
|
76
|
-
const stringWithBoolean = `true is true`;
|
77
|
-
|
78
|
-
// Some simple expressions that are already strings
|
79
|
-
// can be rewritten without a template at all.
|
80
|
-
|
81
|
-
const text = 'a';
|
82
|
-
const wrappedText = text;
|
83
|
-
type Text = 'A';
|
84
|
-
type WrappedText = Text;
|
85
|
-
|
86
|
-
declare const intersectionWithString: string & { _brand: 'test-brand' };
|
87
|
-
const wrappedIntersection = intersectionWithString;
|
88
|
-
type IntersectionWithString = string & { _brand: 'test-brand' };
|
89
|
-
type WrappedIntersection = IntersectionWithString;
|
90
|
-
```
|
91
|
-
|
92
|
-
</TabItem>
|
93
|
-
</Tabs>
|
94
|
-
|
95
|
-
:::info
|
96
|
-
This rule does not aim to flag template literals without substitution expressions that could have been written as an ordinary string.
|
97
|
-
That is to say, this rule will not help you turn `` `this` `` into `"this"`.
|
98
|
-
If you are looking for such a rule, you can configure the [`@stylistic/ts/quotes`](https://eslint.style/rules/ts/quotes) rule to do this.
|
99
|
-
:::
|
100
|
-
|
101
|
-
## When Not To Use It
|
102
|
-
|
103
|
-
When you want to allow string expressions inside template literals.
|
104
|
-
|
105
|
-
## Related To
|
106
|
-
|
107
|
-
- [`restrict-template-expressions`](./restrict-template-expressions.mdx)
|
108
|
-
- [`@stylistic/ts/quotes`](https://eslint.style/rules/ts/quotes)
|