@typescript-eslint/eslint-plugin 6.8.1-alpha.7 → 6.9.0
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/dist/rules/default-param-last.js +1 -1
- package/dist/rules/default-param-last.js.map +1 -1
- package/dist/rules/enum-utils/shared.js +56 -5
- package/dist/rules/enum-utils/shared.js.map +1 -1
- package/dist/rules/explicit-function-return-type.js +1 -1
- package/dist/rules/explicit-function-return-type.js.map +1 -1
- package/dist/rules/naming-convention-utils/enums.js +2 -0
- package/dist/rules/naming-convention-utils/enums.js.map +1 -1
- package/dist/rules/naming-convention-utils/parse-options.js +1 -0
- package/dist/rules/naming-convention-utils/parse-options.js.map +1 -1
- package/dist/rules/no-empty-function.js +1 -1
- package/dist/rules/no-empty-function.js.map +1 -1
- package/dist/rules/no-namespace.js +1 -2
- package/dist/rules/no-namespace.js.map +1 -1
- package/dist/rules/no-non-null-asserted-nullish-coalescing.js +1 -1
- package/dist/rules/no-non-null-asserted-nullish-coalescing.js.map +1 -1
- package/dist/rules/no-redeclare.js.map +1 -1
- package/dist/rules/no-shadow.js +1 -1
- package/dist/rules/no-shadow.js.map +1 -1
- package/dist/rules/no-unsafe-assignment.js +1 -1
- package/dist/rules/no-unsafe-assignment.js.map +1 -1
- package/dist/rules/no-unsafe-enum-comparison.js +29 -0
- package/dist/rules/no-unsafe-enum-comparison.js.map +1 -1
- package/dist/rules/no-unsafe-return.js +2 -1
- package/dist/rules/no-unsafe-return.js.map +1 -1
- package/dist/rules/prefer-readonly.js +2 -1
- package/dist/rules/prefer-readonly.js.map +1 -1
- package/dist/rules/prefer-regexp-exec.js.map +1 -1
- package/dist/util/collectUnusedVariables.js +1 -1
- package/dist/util/collectUnusedVariables.js.map +1 -1
- package/dist/util/getOperatorPrecedence.js.map +1 -1
- package/docs/rules/array-type.md +6 -6
- package/docs/rules/ban-ts-comment.md +10 -8
- package/docs/rules/ban-types.md +1 -57
- package/docs/rules/class-literal-property-style.md +4 -12
- package/docs/rules/class-methods-use-this.md +2 -2
- package/docs/rules/consistent-generic-constructors.md +4 -4
- package/docs/rules/consistent-indexed-object-style.md +4 -12
- package/docs/rules/consistent-type-assertions.md +4 -4
- package/docs/rules/consistent-type-definitions.md +4 -12
- package/docs/rules/consistent-type-exports.md +2 -2
- package/docs/rules/consistent-type-imports.md +5 -5
- package/docs/rules/dot-notation.md +3 -3
- package/docs/rules/explicit-function-return-type.md +14 -14
- package/docs/rules/explicit-member-accessibility.md +15 -15
- package/docs/rules/explicit-module-boundary-types.md +8 -8
- package/docs/rules/lines-between-class-members.md +2 -6
- package/docs/rules/member-ordering.md +34 -34
- package/docs/rules/method-signature-style.md +4 -4
- package/docs/rules/no-array-constructor.md +0 -4
- package/docs/rules/no-base-to-string.md +1 -1
- package/docs/rules/no-confusing-void-expression.md +2 -2
- package/docs/rules/no-empty-function.md +4 -4
- package/docs/rules/no-explicit-any.md +1 -3
- package/docs/rules/no-extraneous-class.md +8 -8
- package/docs/rules/no-floating-promises.md +2 -2
- package/docs/rules/no-inferrable-types.md +2 -2
- package/docs/rules/no-invalid-void-type.md +4 -4
- package/docs/rules/no-magic-numbers.md +8 -24
- package/docs/rules/no-misused-promises.md +6 -6
- package/docs/rules/no-namespace.md +6 -6
- package/docs/rules/no-redeclare.md +2 -2
- package/docs/rules/no-restricted-imports.md +2 -2
- package/docs/rules/no-shadow.md +2 -2
- package/docs/rules/no-type-alias.md +33 -33
- package/docs/rules/no-unnecessary-boolean-literal-compare.md +4 -4
- package/docs/rules/no-unnecessary-condition.md +1 -1
- package/docs/rules/no-unnecessary-type-assertion.md +2 -2
- package/docs/rules/no-unsafe-argument.md +2 -2
- package/docs/rules/no-unsafe-assignment.md +1 -1
- package/docs/rules/no-unsafe-return.md +1 -1
- package/docs/rules/no-use-before-define.md +4 -12
- package/docs/rules/parameter-properties.md +16 -16
- package/docs/rules/prefer-literal-enum-member.md +2 -2
- package/docs/rules/prefer-nullish-coalescing.md +8 -8
- package/docs/rules/prefer-optional-chain.md +15 -15
- package/docs/rules/prefer-readonly-parameter-types.md +46 -27
- package/docs/rules/prefer-readonly.md +11 -3
- package/docs/rules/require-array-sort-compare.md +2 -2
- package/docs/rules/restrict-plus-operands.md +12 -12
- package/docs/rules/restrict-template-expressions.md +7 -7
- package/docs/rules/return-await.md +6 -6
- package/docs/rules/triple-slash-reference.md +6 -6
- package/docs/rules/type-annotation-spacing.md +8 -8
- package/docs/rules/typedef.md +16 -16
- package/docs/rules/unbound-method.md +1 -1
- package/docs/rules/unified-signatures.md +2 -2
- package/package.json +8 -8
|
@@ -112,7 +112,7 @@ The setting accepts the following values:
|
|
|
112
112
|
|
|
113
113
|
Examples of **correct** code for the `{ "allowAliases": "always" }` options:
|
|
114
114
|
|
|
115
|
-
```ts
|
|
115
|
+
```ts option='{ "allowAliases": "always" }' showPlaygroundButton
|
|
116
116
|
// primitives
|
|
117
117
|
type Foo = 'a';
|
|
118
118
|
|
|
@@ -139,7 +139,7 @@ type Foo = Bar & Baz;
|
|
|
139
139
|
|
|
140
140
|
Examples of **incorrect** code for the `{ "allowAliases": "in-unions" }` option:
|
|
141
141
|
|
|
142
|
-
```ts
|
|
142
|
+
```ts option='{ "allowAliases": "in-unions" }' showPlaygroundButton
|
|
143
143
|
// primitives
|
|
144
144
|
type Foo = 'a';
|
|
145
145
|
|
|
@@ -160,7 +160,7 @@ type Foo = Bar & Baz;
|
|
|
160
160
|
|
|
161
161
|
Examples of **correct** code for the `{ "allowAliases": "in-unions" }` option:
|
|
162
162
|
|
|
163
|
-
```ts
|
|
163
|
+
```ts option='{ "allowAliases": "in-unions" }' showPlaygroundButton
|
|
164
164
|
// primitives
|
|
165
165
|
type Foo = 'a' | 'b';
|
|
166
166
|
|
|
@@ -177,7 +177,7 @@ type Foo = Bar | Baz;
|
|
|
177
177
|
|
|
178
178
|
Examples of **incorrect** code for the `{ "allowAliases": "in-intersections" }` option:
|
|
179
179
|
|
|
180
|
-
```ts
|
|
180
|
+
```ts option='{ "allowAliases": "in-intersections" }' showPlaygroundButton
|
|
181
181
|
// primitives
|
|
182
182
|
type Foo = 'a';
|
|
183
183
|
|
|
@@ -200,7 +200,7 @@ type Foo = Bar | Baz;
|
|
|
200
200
|
|
|
201
201
|
Examples of **correct** code for the `{ "allowAliases": "in-intersections" }` option:
|
|
202
202
|
|
|
203
|
-
```ts
|
|
203
|
+
```ts option='{ "allowAliases": "in-intersections" }' showPlaygroundButton
|
|
204
204
|
// primitives
|
|
205
205
|
type Foo = string & string[];
|
|
206
206
|
|
|
@@ -215,7 +215,7 @@ type Foo = Bar & Baz;
|
|
|
215
215
|
|
|
216
216
|
Examples of **incorrect** code for the `{ "allowAliases": "in-unions-and-intersections" }` option:
|
|
217
217
|
|
|
218
|
-
```ts
|
|
218
|
+
```ts option='{ "allowAliases": "in-unions-and-intersections" }' showPlaygroundButton
|
|
219
219
|
// primitives
|
|
220
220
|
type Foo = 'a';
|
|
221
221
|
|
|
@@ -232,7 +232,7 @@ type Foo = Bar;
|
|
|
232
232
|
|
|
233
233
|
Examples of **correct** code for the `{ "allowAliases": "in-unions-and-intersections" }` option:
|
|
234
234
|
|
|
235
|
-
```ts
|
|
235
|
+
```ts option='{ "allowAliases": "in-unions-and-intersections" }' showPlaygroundButton
|
|
236
236
|
// primitives
|
|
237
237
|
type Foo = 'a' | 'b';
|
|
238
238
|
|
|
@@ -263,7 +263,7 @@ The setting accepts the following values:
|
|
|
263
263
|
|
|
264
264
|
Examples of **correct** code for the `{ "allowCallbacks": "always" }` option:
|
|
265
265
|
|
|
266
|
-
```ts
|
|
266
|
+
```ts option='{ "allowCallbacks": "always" }' showPlaygroundButton
|
|
267
267
|
type Foo = () => void;
|
|
268
268
|
|
|
269
269
|
type Foo = (name: string) => string;
|
|
@@ -281,7 +281,7 @@ This applies to conditional types.
|
|
|
281
281
|
|
|
282
282
|
Examples of **correct** code for the `{ "allowConditionalTypes": "always" }` option:
|
|
283
283
|
|
|
284
|
-
```ts
|
|
284
|
+
```ts option='{ "allowConditionalTypes": "always" }' showPlaygroundButton
|
|
285
285
|
type Foo<T> = T extends number ? number : null;
|
|
286
286
|
```
|
|
287
287
|
|
|
@@ -295,7 +295,7 @@ The setting accepts the following values:
|
|
|
295
295
|
|
|
296
296
|
Examples of **correct** code for the `{ "allowConstructors": "always" }` option:
|
|
297
297
|
|
|
298
|
-
```ts
|
|
298
|
+
```ts option='{ "allowConstructors": "always" }' showPlaygroundButton
|
|
299
299
|
type Foo = new () => void;
|
|
300
300
|
```
|
|
301
301
|
|
|
@@ -312,7 +312,7 @@ The setting accepts the following options:
|
|
|
312
312
|
|
|
313
313
|
Examples of **correct** code for the `{ "allowLiterals": "always" }` options:
|
|
314
314
|
|
|
315
|
-
```ts
|
|
315
|
+
```ts option='{ "allowLiterals": "always" }' showPlaygroundButton
|
|
316
316
|
type Foo = {};
|
|
317
317
|
|
|
318
318
|
type Foo = {
|
|
@@ -333,7 +333,7 @@ type Foo = { name: string } & { age: number };
|
|
|
333
333
|
|
|
334
334
|
Examples of **incorrect** code for the `{ "allowLiterals": "in-unions" }` option:
|
|
335
335
|
|
|
336
|
-
```ts
|
|
336
|
+
```ts option='{ "allowLiterals": "in-unions" }' showPlaygroundButton
|
|
337
337
|
type Foo = {};
|
|
338
338
|
|
|
339
339
|
type Foo = {
|
|
@@ -352,13 +352,13 @@ type Foo = { name: string } & { age: number };
|
|
|
352
352
|
|
|
353
353
|
Examples of **correct** code for the `{ "allowLiterals": "in-unions" }` option:
|
|
354
354
|
|
|
355
|
-
```ts
|
|
355
|
+
```ts option='{ "allowLiterals": "in-unions" }' showPlaygroundButton
|
|
356
356
|
type Foo = { name: string } | { age: number };
|
|
357
357
|
```
|
|
358
358
|
|
|
359
359
|
Examples of **incorrect** code for the `{ "allowLiterals": "in-intersections" }` option:
|
|
360
360
|
|
|
361
|
-
```ts
|
|
361
|
+
```ts option='{ "allowLiterals": "in-intersections" }' showPlaygroundButton
|
|
362
362
|
type Foo = {};
|
|
363
363
|
|
|
364
364
|
type Foo = {
|
|
@@ -377,13 +377,13 @@ type Foo = { name: string } | { age: number };
|
|
|
377
377
|
|
|
378
378
|
Examples of **correct** code for the `{ "allowLiterals": "in-intersections" }` option:
|
|
379
379
|
|
|
380
|
-
```ts
|
|
380
|
+
```ts option='{ "allowLiterals": "in-intersections" }' showPlaygroundButton
|
|
381
381
|
type Foo = { name: string } & { age: number };
|
|
382
382
|
```
|
|
383
383
|
|
|
384
384
|
Examples of **incorrect** code for the `{ "allowLiterals": "in-unions-and-intersections" }` option:
|
|
385
385
|
|
|
386
|
-
```ts
|
|
386
|
+
```ts option='{ "allowLiterals": "in-unions-and-intersections" }' showPlaygroundButton
|
|
387
387
|
type Foo = {};
|
|
388
388
|
|
|
389
389
|
type Foo = {
|
|
@@ -400,7 +400,7 @@ type Foo = {
|
|
|
400
400
|
|
|
401
401
|
Examples of **correct** code for the `{ "allowLiterals": "in-unions-and-intersections" }` option:
|
|
402
402
|
|
|
403
|
-
```ts
|
|
403
|
+
```ts option='{ "allowLiterals": "in-unions-and-intersections" }' showPlaygroundButton
|
|
404
404
|
type Foo = { name: string } | { age: number };
|
|
405
405
|
|
|
406
406
|
type Foo = { name: string } & { age: number };
|
|
@@ -419,7 +419,7 @@ The setting accepts the following values:
|
|
|
419
419
|
|
|
420
420
|
Examples of **correct** code for the `{ "allowMappedTypes": "always" }` options:
|
|
421
421
|
|
|
422
|
-
```ts
|
|
422
|
+
```ts option='{ "allowMappedTypes": "always" }' showPlaygroundButton
|
|
423
423
|
type Foo<T> = { readonly [P in keyof T]: T[P] };
|
|
424
424
|
|
|
425
425
|
type Foo<T> = { [P in keyof T]?: T[P] };
|
|
@@ -439,7 +439,7 @@ type Foo<T, U> = { [P in keyof T]?: T[P] } & { [P in keyof U]?: U[P] };
|
|
|
439
439
|
|
|
440
440
|
Examples of **incorrect** code for the `{ "allowMappedTypes": "in-unions" }` option:
|
|
441
441
|
|
|
442
|
-
```ts
|
|
442
|
+
```ts option='{ "allowMappedTypes": "in-unions" }' showPlaygroundButton
|
|
443
443
|
type Foo<T> = { readonly [P in keyof T]: T[P] };
|
|
444
444
|
|
|
445
445
|
type Foo<T> = { [P in keyof T]?: T[P] };
|
|
@@ -453,7 +453,7 @@ type Foo<T, U> = { [P in keyof T]?: T[P] } & { [P in keyof U]?: U[P] };
|
|
|
453
453
|
|
|
454
454
|
Examples of **correct** code for the `{ "allowMappedTypes": "in-unions" }` option:
|
|
455
455
|
|
|
456
|
-
```ts
|
|
456
|
+
```ts option='{ "allowMappedTypes": "in-unions" }' showPlaygroundButton
|
|
457
457
|
type Foo<T, U> =
|
|
458
458
|
| { readonly [P in keyof T]: T[P] }
|
|
459
459
|
| { readonly [P in keyof U]: U[P] };
|
|
@@ -463,7 +463,7 @@ type Foo<T, U> = { [P in keyof T]?: T[P] } | { [P in keyof U]?: U[P] };
|
|
|
463
463
|
|
|
464
464
|
Examples of **incorrect** code for the `{ "allowMappedTypes": "in-intersections" }` option:
|
|
465
465
|
|
|
466
|
-
```ts
|
|
466
|
+
```ts option='{ "allowMappedTypes": "in-intersections" }' showPlaygroundButton
|
|
467
467
|
type Foo<T> = { readonly [P in keyof T]: T[P] };
|
|
468
468
|
|
|
469
469
|
type Foo<T> = { [P in keyof T]?: T[P] };
|
|
@@ -477,7 +477,7 @@ type Foo<T, U> = { [P in keyof T]?: T[P] } | { [P in keyof U]?: U[P] };
|
|
|
477
477
|
|
|
478
478
|
Examples of **correct** code for the `{ "allowMappedTypes": "in-intersections" }` option:
|
|
479
479
|
|
|
480
|
-
```ts
|
|
480
|
+
```ts option='{ "allowMappedTypes": "in-intersections" }' showPlaygroundButton
|
|
481
481
|
type Foo<T, U> = { readonly [P in keyof T]: T[P] } & {
|
|
482
482
|
readonly [P in keyof U]: U[P];
|
|
483
483
|
};
|
|
@@ -487,7 +487,7 @@ type Foo<T, U> = { [P in keyof T]?: T[P] } & { [P in keyof U]?: U[P] };
|
|
|
487
487
|
|
|
488
488
|
Examples of **incorrect** code for the `{ "allowMappedTypes": "in-unions-and-intersections" }` option:
|
|
489
489
|
|
|
490
|
-
```ts
|
|
490
|
+
```ts option='{ "allowMappedTypes": "in-unions-and-intersections" }' showPlaygroundButton
|
|
491
491
|
type Foo<T> = { readonly [P in keyof T]: T[P] };
|
|
492
492
|
|
|
493
493
|
type Foo<T> = { [P in keyof T]?: T[P] };
|
|
@@ -495,7 +495,7 @@ type Foo<T> = { [P in keyof T]?: T[P] };
|
|
|
495
495
|
|
|
496
496
|
Examples of **correct** code for the `{ "allowMappedTypes": "in-unions-and-intersections" }` option:
|
|
497
497
|
|
|
498
|
-
```ts
|
|
498
|
+
```ts option='{ "allowMappedTypes": "in-unions-and-intersections" }' showPlaygroundButton
|
|
499
499
|
type Foo<T, U> =
|
|
500
500
|
| { readonly [P in keyof T]: T[P] }
|
|
501
501
|
| { readonly [P in keyof U]: U[P] };
|
|
@@ -522,7 +522,7 @@ The setting accepts the following options:
|
|
|
522
522
|
|
|
523
523
|
Examples of **correct** code for the `{ "allowTupleTypes": "always" }` options:
|
|
524
524
|
|
|
525
|
-
```ts
|
|
525
|
+
```ts option='{ "allowTupleTypes": "always" }' showPlaygroundButton
|
|
526
526
|
type Foo = [number];
|
|
527
527
|
|
|
528
528
|
type Foo = [number] | [number, number];
|
|
@@ -534,7 +534,7 @@ type Foo = [number] | ([number, number] & [string, string]);
|
|
|
534
534
|
|
|
535
535
|
Examples of **incorrect** code for the `{ "allowTupleTypes": "in-unions" }` option:
|
|
536
536
|
|
|
537
|
-
```ts
|
|
537
|
+
```ts option='{ "allowTupleTypes": "in-unions" }' showPlaygroundButton
|
|
538
538
|
type Foo = [number];
|
|
539
539
|
|
|
540
540
|
type Foo = [number] & [number, number];
|
|
@@ -544,7 +544,7 @@ type Foo = [string] & [number];
|
|
|
544
544
|
|
|
545
545
|
Examples of **correct** code for the `{ "allowTupleTypes": "in-unions" }` option:
|
|
546
546
|
|
|
547
|
-
```ts
|
|
547
|
+
```ts option='{ "allowTupleTypes": "in-unions" }' showPlaygroundButton
|
|
548
548
|
type Foo = [number] | [number, number];
|
|
549
549
|
|
|
550
550
|
type Foo = [string] | [number];
|
|
@@ -552,7 +552,7 @@ type Foo = [string] | [number];
|
|
|
552
552
|
|
|
553
553
|
Examples of **incorrect** code for the `{ "allowTupleTypes": "in-intersections" }` option:
|
|
554
554
|
|
|
555
|
-
```ts
|
|
555
|
+
```ts option='{ "allowTupleTypes": "in-intersections" }' showPlaygroundButton
|
|
556
556
|
type Foo = [number];
|
|
557
557
|
|
|
558
558
|
type Foo = [number] | [number, number];
|
|
@@ -562,7 +562,7 @@ type Foo = [string] | [number];
|
|
|
562
562
|
|
|
563
563
|
Examples of **correct** code for the `{ "allowTupleTypes": "in-intersections" }` option:
|
|
564
564
|
|
|
565
|
-
```ts
|
|
565
|
+
```ts option='{ "allowTupleTypes": "in-intersections" }' showPlaygroundButton
|
|
566
566
|
type Foo = [number] & [number, number];
|
|
567
567
|
|
|
568
568
|
type Foo = [string] & [number];
|
|
@@ -570,15 +570,15 @@ type Foo = [string] & [number];
|
|
|
570
570
|
|
|
571
571
|
Examples of **incorrect** code for the `{ "allowTupleTypes": "in-unions-and-intersections" }` option:
|
|
572
572
|
|
|
573
|
-
```ts
|
|
573
|
+
```ts option='{ "allowTupleTypes": "in-unions-and-intersections" }' showPlaygroundButton
|
|
574
574
|
type Foo = [number];
|
|
575
575
|
|
|
576
576
|
type Foo = [string];
|
|
577
577
|
```
|
|
578
578
|
|
|
579
|
-
Examples of **correct** code for the `{ "
|
|
579
|
+
Examples of **correct** code for the `{ "allowTupleTypes": "in-unions-and-intersections" }` option:
|
|
580
580
|
|
|
581
|
-
```ts
|
|
581
|
+
```ts option='{ "allowTupleTypes": "in-unions-and-intersections" }' showPlaygroundButton
|
|
582
582
|
type Foo = [number] & [number, number];
|
|
583
583
|
|
|
584
584
|
type Foo = [string] | [number];
|
|
@@ -594,7 +594,7 @@ The setting accepts the following options:
|
|
|
594
594
|
|
|
595
595
|
Examples of **correct** code for the `{ "allowGenerics": "always" }` options:
|
|
596
596
|
|
|
597
|
-
```ts
|
|
597
|
+
```ts option='{ "allowGenerics": "always" }' showPlaygroundButton
|
|
598
598
|
type Foo = Bar<string>;
|
|
599
599
|
|
|
600
600
|
type Foo = Record<string, number>;
|
|
@@ -61,7 +61,7 @@ Examples of code for this rule with `{ allowComparingNullableBooleansToTrue: fal
|
|
|
61
61
|
|
|
62
62
|
#### ❌ Incorrect
|
|
63
63
|
|
|
64
|
-
```ts
|
|
64
|
+
```ts option='{ "allowComparingNullableBooleansToTrue": false }'
|
|
65
65
|
declare const someUndefinedCondition: boolean | undefined;
|
|
66
66
|
if (someUndefinedCondition === true) {
|
|
67
67
|
}
|
|
@@ -73,7 +73,7 @@ if (someNullCondition !== true) {
|
|
|
73
73
|
|
|
74
74
|
#### ✅ Correct
|
|
75
75
|
|
|
76
|
-
```ts
|
|
76
|
+
```ts option='{ "allowComparingNullableBooleansToTrue": false }'
|
|
77
77
|
declare const someUndefinedCondition: boolean | undefined;
|
|
78
78
|
if (someUndefinedCondition) {
|
|
79
79
|
}
|
|
@@ -91,7 +91,7 @@ Examples of code for this rule with `{ allowComparingNullableBooleansToFalse: fa
|
|
|
91
91
|
|
|
92
92
|
#### ❌ Incorrect
|
|
93
93
|
|
|
94
|
-
```ts
|
|
94
|
+
```ts option='{ "allowComparingNullableBooleansToFalse": false }'
|
|
95
95
|
declare const someUndefinedCondition: boolean | undefined;
|
|
96
96
|
if (someUndefinedCondition === false) {
|
|
97
97
|
}
|
|
@@ -103,7 +103,7 @@ if (someNullCondition !== false) {
|
|
|
103
103
|
|
|
104
104
|
#### ✅ Correct
|
|
105
105
|
|
|
106
|
-
```ts
|
|
106
|
+
```ts option='{ "allowComparingNullableBooleansToFalse": false }'
|
|
107
107
|
declare const someUndefinedCondition: boolean | undefined;
|
|
108
108
|
if (someUndefinedCondition ?? true) {
|
|
109
109
|
}
|
|
@@ -77,7 +77,7 @@ function bar(arg?: string | null) {
|
|
|
77
77
|
|
|
78
78
|
Example of correct code for `{ allowConstantLoopConditions: true }`:
|
|
79
79
|
|
|
80
|
-
```ts
|
|
80
|
+
```ts option='{ "allowConstantLoopConditions": true }' showPlaygroundButton
|
|
81
81
|
while (true) {}
|
|
82
82
|
for (; true; ) {}
|
|
83
83
|
do {} while (true);
|
|
@@ -65,9 +65,9 @@ function foo(x: number | undefined): number {
|
|
|
65
65
|
|
|
66
66
|
### `typesToIgnore`
|
|
67
67
|
|
|
68
|
-
With `@typescript-eslint/no-unnecessary-type-assertion: ["error", { typesToIgnore: ['Foo'] }]`, the following is **correct** code
|
|
68
|
+
With `@typescript-eslint/no-unnecessary-type-assertion: ["error", { typesToIgnore: ['Foo'] }]`, the following is **correct** code:
|
|
69
69
|
|
|
70
|
-
```ts
|
|
70
|
+
```ts option='{ "typesToIgnore": ["Foo"] }' showPlaygroundButton
|
|
71
71
|
type Foo = 3;
|
|
72
72
|
const foo: Foo = 3;
|
|
73
73
|
```
|
|
@@ -73,8 +73,8 @@ There are cases where the rule allows passing an argument of `any` to `unknown`.
|
|
|
73
73
|
|
|
74
74
|
Example of `any` to `unknown` assignment that are allowed:
|
|
75
75
|
|
|
76
|
-
```ts
|
|
77
|
-
declare function foo(arg1: unknown, arg2: Set<
|
|
76
|
+
```ts showPlaygroundButton
|
|
77
|
+
declare function foo(arg1: unknown, arg2: Set<unknown>, arg3: unknown[]): void;
|
|
78
78
|
foo(1 as any, new Set<any>(), [] as any[]);
|
|
79
79
|
```
|
|
80
80
|
|
|
@@ -75,7 +75,7 @@ There are cases where the rule allows assignment of `any` to `unknown`.
|
|
|
75
75
|
|
|
76
76
|
Example of `any` to `unknown` assignment that are allowed:
|
|
77
77
|
|
|
78
|
-
```ts
|
|
78
|
+
```ts showPlaygroundButton
|
|
79
79
|
const x: unknown = y as any;
|
|
80
80
|
const x: unknown[] = y as any[];
|
|
81
81
|
const x: Set<unknown> = y as Set<any>;
|
|
@@ -88,7 +88,7 @@ There are cases where the rule allows to return `any` to `unknown`.
|
|
|
88
88
|
|
|
89
89
|
Examples of `any` to `unknown` return that are allowed:
|
|
90
90
|
|
|
91
|
-
```ts
|
|
91
|
+
```ts showPlaygroundButton
|
|
92
92
|
function foo1(): unknown {
|
|
93
93
|
return JSON.parse(singleObjString); // Return type for JSON.parse is any.
|
|
94
94
|
}
|
|
@@ -39,9 +39,7 @@ Examples of code for the `{ "enums": true }` option:
|
|
|
39
39
|
|
|
40
40
|
#### ❌ Incorrect
|
|
41
41
|
|
|
42
|
-
```ts
|
|
43
|
-
/*eslint no-use-before-define: ["error", { "enums": true }]*/
|
|
44
|
-
|
|
42
|
+
```ts option='{ "enums": true }'
|
|
45
43
|
const x = Foo.FOO;
|
|
46
44
|
|
|
47
45
|
enum Foo {
|
|
@@ -51,9 +49,7 @@ enum Foo {
|
|
|
51
49
|
|
|
52
50
|
#### ✅ Correct
|
|
53
51
|
|
|
54
|
-
```ts
|
|
55
|
-
/*eslint no-use-before-define: ["error", { "enums": false }]*/
|
|
56
|
-
|
|
52
|
+
```ts option='{ "enums": false }'
|
|
57
53
|
function foo() {
|
|
58
54
|
return Foo.FOO;
|
|
59
55
|
}
|
|
@@ -70,9 +66,7 @@ If this is `false`, this rule will ignore references to types.
|
|
|
70
66
|
|
|
71
67
|
Examples of **correct** code for the `{ "typedefs": false }` option:
|
|
72
68
|
|
|
73
|
-
```ts
|
|
74
|
-
/*eslint no-use-before-define: ["error", { "typedefs": false }]*/
|
|
75
|
-
|
|
69
|
+
```ts option='{ "typedefs": false }' showPlaygroundButton
|
|
76
70
|
let myVar: StringOrNumber;
|
|
77
71
|
type StringOrNumber = string | number;
|
|
78
72
|
```
|
|
@@ -84,9 +78,7 @@ If this is `false`, this will will check all type references.
|
|
|
84
78
|
|
|
85
79
|
Examples of **correct** code for the `{ "ignoreTypeReferences": true }` option:
|
|
86
80
|
|
|
87
|
-
```ts
|
|
88
|
-
/*eslint no-use-before-define: ["error", { "ignoreTypeReferences": true }]*/
|
|
89
|
-
|
|
81
|
+
```ts option='{ "ignoreTypeReferences": true }' showPlaygroundButton
|
|
90
82
|
let var1: StringOrNumber;
|
|
91
83
|
type StringOrNumber = string | number;
|
|
92
84
|
|
|
@@ -109,7 +109,7 @@ Examples of code for the `{ "allow": ["readonly"] }` options:
|
|
|
109
109
|
|
|
110
110
|
#### ❌ Incorrect
|
|
111
111
|
|
|
112
|
-
```ts
|
|
112
|
+
```ts option='{ "allow": ["readonly"] }'
|
|
113
113
|
class Foo {
|
|
114
114
|
constructor(private name: string) {}
|
|
115
115
|
}
|
|
@@ -137,7 +137,7 @@ class Foo {
|
|
|
137
137
|
|
|
138
138
|
#### ✅ Correct
|
|
139
139
|
|
|
140
|
-
```ts
|
|
140
|
+
```ts option='{ "allow": ["readonly"] }'
|
|
141
141
|
class Foo {
|
|
142
142
|
constructor(name: string) {}
|
|
143
143
|
}
|
|
@@ -155,7 +155,7 @@ Examples of code for the `{ "allow": ["private"] }` options:
|
|
|
155
155
|
|
|
156
156
|
#### ❌ Incorrect
|
|
157
157
|
|
|
158
|
-
```ts
|
|
158
|
+
```ts option='{ "allow": ["private"] }'
|
|
159
159
|
class Foo {
|
|
160
160
|
constructor(readonly name: string) {}
|
|
161
161
|
}
|
|
@@ -183,7 +183,7 @@ class Foo {
|
|
|
183
183
|
|
|
184
184
|
#### ✅ Correct
|
|
185
185
|
|
|
186
|
-
```ts
|
|
186
|
+
```ts option='{ "allow": ["private"] }'
|
|
187
187
|
class Foo {
|
|
188
188
|
constructor(name: string) {}
|
|
189
189
|
}
|
|
@@ -201,7 +201,7 @@ Examples of code for the `{ "allow": ["protected"] }` options:
|
|
|
201
201
|
|
|
202
202
|
#### ❌ Incorrect
|
|
203
203
|
|
|
204
|
-
```ts
|
|
204
|
+
```ts option='{ "allow": ["protected"] }'
|
|
205
205
|
class Foo {
|
|
206
206
|
constructor(readonly name: string) {}
|
|
207
207
|
}
|
|
@@ -229,7 +229,7 @@ class Foo {
|
|
|
229
229
|
|
|
230
230
|
#### ✅ Correct
|
|
231
231
|
|
|
232
|
-
```ts
|
|
232
|
+
```ts option='{ "allow": ["protected"] }'
|
|
233
233
|
class Foo {
|
|
234
234
|
constructor(name: string) {}
|
|
235
235
|
}
|
|
@@ -247,7 +247,7 @@ Examples of code for the `{ "allow": ["public"] }` options:
|
|
|
247
247
|
|
|
248
248
|
#### ❌ Incorrect
|
|
249
249
|
|
|
250
|
-
```ts
|
|
250
|
+
```ts option='{ "allow": ["public"] }'
|
|
251
251
|
class Foo {
|
|
252
252
|
constructor(readonly name: string) {}
|
|
253
253
|
}
|
|
@@ -275,7 +275,7 @@ class Foo {
|
|
|
275
275
|
|
|
276
276
|
#### ✅ Correct
|
|
277
277
|
|
|
278
|
-
```ts
|
|
278
|
+
```ts option='{ "allow": ["public"] }'
|
|
279
279
|
class Foo {
|
|
280
280
|
constructor(name: string) {}
|
|
281
281
|
}
|
|
@@ -293,7 +293,7 @@ Examples of code for the `{ "allow": ["private readonly"] }` options:
|
|
|
293
293
|
|
|
294
294
|
#### ❌ Incorrect
|
|
295
295
|
|
|
296
|
-
```ts
|
|
296
|
+
```ts option='{ "allow": ["private readonly"] }'
|
|
297
297
|
class Foo {
|
|
298
298
|
constructor(readonly name: string) {}
|
|
299
299
|
}
|
|
@@ -321,7 +321,7 @@ class Foo {
|
|
|
321
321
|
|
|
322
322
|
#### ✅ Correct
|
|
323
323
|
|
|
324
|
-
```ts
|
|
324
|
+
```ts option='{ "allow": ["private readonly"] }'
|
|
325
325
|
class Foo {
|
|
326
326
|
constructor(name: string) {}
|
|
327
327
|
}
|
|
@@ -339,7 +339,7 @@ Examples of code for the `{ "allow": ["protected readonly"] }` options:
|
|
|
339
339
|
|
|
340
340
|
#### ❌ Incorrect
|
|
341
341
|
|
|
342
|
-
```ts
|
|
342
|
+
```ts option='{ "allow": ["protected readonly"] }'
|
|
343
343
|
class Foo {
|
|
344
344
|
constructor(readonly name: string) {}
|
|
345
345
|
}
|
|
@@ -367,7 +367,7 @@ class Foo {
|
|
|
367
367
|
|
|
368
368
|
#### ✅ Correct
|
|
369
369
|
|
|
370
|
-
```ts
|
|
370
|
+
```ts option='{ "allow": ["protected readonly"] }'
|
|
371
371
|
class Foo {
|
|
372
372
|
constructor(name: string) {}
|
|
373
373
|
}
|
|
@@ -385,7 +385,7 @@ Examples of code for the `{ "allow": ["public readonly"] }` options:
|
|
|
385
385
|
|
|
386
386
|
#### ❌ Incorrect
|
|
387
387
|
|
|
388
|
-
```ts
|
|
388
|
+
```ts option='{ "allow": ["public readonly"] }'
|
|
389
389
|
class Foo {
|
|
390
390
|
constructor(readonly name: string) {}
|
|
391
391
|
}
|
|
@@ -413,7 +413,7 @@ class Foo {
|
|
|
413
413
|
|
|
414
414
|
#### ✅ Correct
|
|
415
415
|
|
|
416
|
-
```ts
|
|
416
|
+
```ts option='{ "allow": ["public readonly"] }'
|
|
417
417
|
class Foo {
|
|
418
418
|
constructor(name: string) {}
|
|
419
419
|
}
|
|
@@ -431,7 +431,7 @@ Examples of code for the `{ "prefer": "parameter-property" }` option:
|
|
|
431
431
|
|
|
432
432
|
#### ❌ Incorrect
|
|
433
433
|
|
|
434
|
-
```ts
|
|
434
|
+
```ts option='{ "prefer": "parameter-property" }'
|
|
435
435
|
class Foo {
|
|
436
436
|
private name: string;
|
|
437
437
|
constructor(name: string) {
|
|
@@ -456,7 +456,7 @@ class Foo {
|
|
|
456
456
|
|
|
457
457
|
#### ✅ Correct
|
|
458
458
|
|
|
459
|
-
```ts
|
|
459
|
+
```ts option='{ "prefer": "parameter-property" }'
|
|
460
460
|
class Foo {
|
|
461
461
|
private differentName: string;
|
|
462
462
|
constructor(name: string) {
|
|
@@ -69,7 +69,7 @@ Examples of code for the `{ "allowBitwiseExpressions": true }` option:
|
|
|
69
69
|
|
|
70
70
|
### ❌ Incorrect
|
|
71
71
|
|
|
72
|
-
```ts
|
|
72
|
+
```ts option='{ "allowBitwiseExpressions": true }'
|
|
73
73
|
const x = 1;
|
|
74
74
|
enum Foo {
|
|
75
75
|
A = x << 0,
|
|
@@ -84,7 +84,7 @@ enum Foo {
|
|
|
84
84
|
|
|
85
85
|
### ✅ Correct
|
|
86
86
|
|
|
87
|
-
```ts
|
|
87
|
+
```ts option='{ "allowBitwiseExpressions": true }'
|
|
88
88
|
enum Foo {
|
|
89
89
|
A = 1 << 0,
|
|
90
90
|
B = 1 >> 0,
|
|
@@ -26,7 +26,7 @@ Setting this option to `true` will cause the rule to ignore any ternary expressi
|
|
|
26
26
|
|
|
27
27
|
Incorrect code for `ignoreTernaryTests: false`, and correct code for `ignoreTernaryTests: true`:
|
|
28
28
|
|
|
29
|
-
```ts
|
|
29
|
+
```ts option='{ "ignoreTernaryTests": false }' showPlaygroundButton
|
|
30
30
|
const foo: any = 'bar';
|
|
31
31
|
foo !== undefined && foo !== null ? foo : 'a string';
|
|
32
32
|
foo === undefined || foo === null ? 'a string' : foo;
|
|
@@ -44,7 +44,7 @@ foo === null ? 'a string' : foo;
|
|
|
44
44
|
|
|
45
45
|
Correct code for `ignoreTernaryTests: false`:
|
|
46
46
|
|
|
47
|
-
```ts
|
|
47
|
+
```ts option='{ "ignoreTernaryTests": false }' showPlaygroundButton
|
|
48
48
|
const foo: any = 'bar';
|
|
49
49
|
foo ?? 'a string';
|
|
50
50
|
foo ?? 'a string';
|
|
@@ -70,7 +70,7 @@ If you're looking to enforce stricter conditional tests, you should consider usi
|
|
|
70
70
|
|
|
71
71
|
Incorrect code for `ignoreConditionalTests: false`, and correct code for `ignoreConditionalTests: true`:
|
|
72
72
|
|
|
73
|
-
```ts
|
|
73
|
+
```ts option='{ "ignoreConditionalTests": false }' showPlaygroundButton
|
|
74
74
|
declare const a: string | null;
|
|
75
75
|
declare const b: string | null;
|
|
76
76
|
|
|
@@ -87,7 +87,7 @@ a || b ? true : false;
|
|
|
87
87
|
|
|
88
88
|
Correct code for `ignoreConditionalTests: false`:
|
|
89
89
|
|
|
90
|
-
```ts
|
|
90
|
+
```ts option='{ "ignoreConditionalTests": false }' showPlaygroundButton
|
|
91
91
|
declare const a: string | null;
|
|
92
92
|
declare const b: string | null;
|
|
93
93
|
|
|
@@ -112,7 +112,7 @@ If you're looking to enforce stricter conditional tests, you should consider usi
|
|
|
112
112
|
|
|
113
113
|
Incorrect code for `ignoreMixedLogicalExpressions: false`, and correct code for `ignoreMixedLogicalExpressions: true`:
|
|
114
114
|
|
|
115
|
-
```ts
|
|
115
|
+
```ts option='{ "ignoreMixedLogicalExpressions": false }' showPlaygroundButton
|
|
116
116
|
declare const a: string | null;
|
|
117
117
|
declare const b: string | null;
|
|
118
118
|
declare const c: string | null;
|
|
@@ -127,7 +127,7 @@ a || (b && c && d);
|
|
|
127
127
|
|
|
128
128
|
Correct code for `ignoreMixedLogicalExpressions: false`:
|
|
129
129
|
|
|
130
|
-
```ts
|
|
130
|
+
```ts option='{ "ignoreMixedLogicalExpressions": false }' showPlaygroundButton
|
|
131
131
|
declare const a: string | null;
|
|
132
132
|
declare const b: string | null;
|
|
133
133
|
declare const c: string | null;
|
|
@@ -153,14 +153,14 @@ If you would like to ignore expressions containing operands of certain primitive
|
|
|
153
153
|
|
|
154
154
|
Incorrect code for `ignorePrimitives: { string: false }`, and correct code for `ignorePrimitives: { string: true }`:
|
|
155
155
|
|
|
156
|
-
```ts
|
|
156
|
+
```ts option='{ "ignorePrimitives": { "string": true } }' showPlaygroundButton
|
|
157
157
|
const foo: string | undefined = 'bar';
|
|
158
158
|
foo || 'a string';
|
|
159
159
|
```
|
|
160
160
|
|
|
161
161
|
Correct code for both `ignorePrimitives: { string: false }` and `ignorePrimitives: { string: true }`:
|
|
162
162
|
|
|
163
|
-
```ts
|
|
163
|
+
```ts option='{ "ignorePrimitives": { "string": true } }' showPlaygroundButton
|
|
164
164
|
const foo: string | undefined = 'bar';
|
|
165
165
|
foo ?? 'a string';
|
|
166
166
|
```
|