@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.
Files changed (161) hide show
  1. package/package.json +9 -10
  2. package/docs/rules/README.md +0 -57
  3. package/docs/rules/TEMPLATE.md +0 -36
  4. package/docs/rules/adjacent-overload-signatures.mdx +0 -105
  5. package/docs/rules/array-type.mdx +0 -126
  6. package/docs/rules/await-thenable.mdx +0 -184
  7. package/docs/rules/ban-ts-comment.mdx +0 -165
  8. package/docs/rules/ban-tslint-comment.mdx +0 -45
  9. package/docs/rules/ban-types.md +0 -26
  10. package/docs/rules/block-spacing.md +0 -15
  11. package/docs/rules/brace-style.md +0 -15
  12. package/docs/rules/camelcase.md +0 -15
  13. package/docs/rules/class-literal-property-style.mdx +0 -112
  14. package/docs/rules/class-methods-use-this.mdx +0 -135
  15. package/docs/rules/comma-dangle.md +0 -15
  16. package/docs/rules/comma-spacing.md +0 -15
  17. package/docs/rules/consistent-generic-constructors.mdx +0 -87
  18. package/docs/rules/consistent-indexed-object-style.mdx +0 -105
  19. package/docs/rules/consistent-return.mdx +0 -51
  20. package/docs/rules/consistent-type-assertions.mdx +0 -196
  21. package/docs/rules/consistent-type-definitions.mdx +0 -133
  22. package/docs/rules/consistent-type-exports.mdx +0 -97
  23. package/docs/rules/consistent-type-imports.mdx +0 -139
  24. package/docs/rules/default-param-last.mdx +0 -59
  25. package/docs/rules/dot-notation.mdx +0 -94
  26. package/docs/rules/explicit-function-return-type.mdx +0 -359
  27. package/docs/rules/explicit-member-accessibility.mdx +0 -353
  28. package/docs/rules/explicit-module-boundary-types.mdx +0 -287
  29. package/docs/rules/func-call-spacing.md +0 -15
  30. package/docs/rules/indent.md +0 -15
  31. package/docs/rules/init-declarations.mdx +0 -12
  32. package/docs/rules/key-spacing.md +0 -15
  33. package/docs/rules/keyword-spacing.md +0 -15
  34. package/docs/rules/lines-around-comment.md +0 -15
  35. package/docs/rules/lines-between-class-members.md +0 -15
  36. package/docs/rules/max-params.mdx +0 -54
  37. package/docs/rules/member-delimiter-style.md +0 -15
  38. package/docs/rules/member-ordering.mdx +0 -1483
  39. package/docs/rules/method-signature-style.mdx +0 -124
  40. package/docs/rules/naming-convention.mdx +0 -755
  41. package/docs/rules/no-array-constructor.mdx +0 -34
  42. package/docs/rules/no-array-delete.mdx +0 -44
  43. package/docs/rules/no-base-to-string.mdx +0 -115
  44. package/docs/rules/no-confusing-non-null-assertion.mdx +0 -75
  45. package/docs/rules/no-confusing-void-expression.mdx +0 -148
  46. package/docs/rules/no-deprecated.mdx +0 -119
  47. package/docs/rules/no-dupe-class-members.mdx +0 -16
  48. package/docs/rules/no-duplicate-enum-values.mdx +0 -66
  49. package/docs/rules/no-duplicate-imports.mdx +0 -17
  50. package/docs/rules/no-duplicate-type-constituents.mdx +0 -89
  51. package/docs/rules/no-dynamic-delete.mdx +0 -64
  52. package/docs/rules/no-empty-function.mdx +0 -94
  53. package/docs/rules/no-empty-interface.mdx +0 -75
  54. package/docs/rules/no-empty-object-type.mdx +0 -150
  55. package/docs/rules/no-explicit-any.mdx +0 -177
  56. package/docs/rules/no-extra-non-null-assertion.mdx +0 -60
  57. package/docs/rules/no-extra-parens.md +0 -15
  58. package/docs/rules/no-extra-semi.md +0 -15
  59. package/docs/rules/no-extraneous-class.mdx +0 -329
  60. package/docs/rules/no-floating-promises.mdx +0 -282
  61. package/docs/rules/no-for-in-array.mdx +0 -67
  62. package/docs/rules/no-implied-eval.mdx +0 -106
  63. package/docs/rules/no-import-type-side-effects.mdx +0 -80
  64. package/docs/rules/no-inferrable-types.mdx +0 -113
  65. package/docs/rules/no-invalid-this.mdx +0 -16
  66. package/docs/rules/no-invalid-void-type.mdx +0 -119
  67. package/docs/rules/no-loop-func.mdx +0 -12
  68. package/docs/rules/no-loss-of-precision.mdx +0 -17
  69. package/docs/rules/no-magic-numbers.mdx +0 -131
  70. package/docs/rules/no-meaningless-void-operator.mdx +0 -61
  71. package/docs/rules/no-misused-new.mdx +0 -53
  72. package/docs/rules/no-misused-promises.mdx +0 -314
  73. package/docs/rules/no-misused-spread.mdx +0 -132
  74. package/docs/rules/no-mixed-enums.mdx +0 -96
  75. package/docs/rules/no-namespace.mdx +0 -157
  76. package/docs/rules/no-non-null-asserted-nullish-coalescing.mdx +0 -60
  77. package/docs/rules/no-non-null-asserted-optional-chain.mdx +0 -46
  78. package/docs/rules/no-non-null-assertion.mdx +0 -48
  79. package/docs/rules/no-parameter-properties.mdx +0 -16
  80. package/docs/rules/no-redeclare.mdx +0 -79
  81. package/docs/rules/no-redundant-type-constituents.mdx +0 -102
  82. package/docs/rules/no-require-imports.mdx +0 -114
  83. package/docs/rules/no-restricted-imports.mdx +0 -84
  84. package/docs/rules/no-restricted-types.mdx +0 -70
  85. package/docs/rules/no-shadow.mdx +0 -143
  86. package/docs/rules/no-this-alias.mdx +0 -124
  87. package/docs/rules/no-type-alias.mdx +0 -626
  88. package/docs/rules/no-unnecessary-boolean-literal-compare.mdx +0 -165
  89. package/docs/rules/no-unnecessary-condition.mdx +0 -293
  90. package/docs/rules/no-unnecessary-parameter-property-assignment.mdx +0 -42
  91. package/docs/rules/no-unnecessary-qualifier.mdx +0 -57
  92. package/docs/rules/no-unnecessary-template-expression.mdx +0 -108
  93. package/docs/rules/no-unnecessary-type-arguments.mdx +0 -85
  94. package/docs/rules/no-unnecessary-type-assertion.mdx +0 -97
  95. package/docs/rules/no-unnecessary-type-constraint.mdx +0 -61
  96. package/docs/rules/no-unnecessary-type-conversion.mdx +0 -79
  97. package/docs/rules/no-unnecessary-type-parameters.mdx +0 -255
  98. package/docs/rules/no-unsafe-argument.mdx +0 -98
  99. package/docs/rules/no-unsafe-assignment.mdx +0 -101
  100. package/docs/rules/no-unsafe-call.mdx +0 -120
  101. package/docs/rules/no-unsafe-declaration-merging.mdx +0 -65
  102. package/docs/rules/no-unsafe-enum-comparison.mdx +0 -98
  103. package/docs/rules/no-unsafe-function-type.mdx +0 -65
  104. package/docs/rules/no-unsafe-member-access.mdx +0 -81
  105. package/docs/rules/no-unsafe-return.mdx +0 -126
  106. package/docs/rules/no-unsafe-type-assertion.mdx +0 -63
  107. package/docs/rules/no-unsafe-unary-minus.mdx +0 -60
  108. package/docs/rules/no-unused-expressions.mdx +0 -52
  109. package/docs/rules/no-unused-vars.mdx +0 -136
  110. package/docs/rules/no-use-before-define.mdx +0 -98
  111. package/docs/rules/no-useless-constructor.mdx +0 -21
  112. package/docs/rules/no-useless-empty-export.mdx +0 -53
  113. package/docs/rules/no-useless-template-literals.mdx +0 -9
  114. package/docs/rules/no-var-requires.mdx +0 -77
  115. package/docs/rules/no-wrapper-object-types.mdx +0 -75
  116. package/docs/rules/non-nullable-type-assertion-style.mdx +0 -47
  117. package/docs/rules/object-curly-spacing.md +0 -15
  118. package/docs/rules/only-throw-error.mdx +0 -150
  119. package/docs/rules/padding-line-between-statements.md +0 -15
  120. package/docs/rules/parameter-properties.mdx +0 -522
  121. package/docs/rules/prefer-as-const.mdx +0 -51
  122. package/docs/rules/prefer-destructuring.mdx +0 -101
  123. package/docs/rules/prefer-enum-initializers.mdx +0 -68
  124. package/docs/rules/prefer-find.mdx +0 -45
  125. package/docs/rules/prefer-for-of.mdx +0 -50
  126. package/docs/rules/prefer-function-type.mdx +0 -98
  127. package/docs/rules/prefer-includes.mdx +0 -81
  128. package/docs/rules/prefer-literal-enum-member.mdx +0 -111
  129. package/docs/rules/prefer-namespace-keyword.mdx +0 -51
  130. package/docs/rules/prefer-nullish-coalescing.mdx +0 -349
  131. package/docs/rules/prefer-optional-chain.mdx +0 -304
  132. package/docs/rules/prefer-promise-reject-errors.mdx +0 -78
  133. package/docs/rules/prefer-readonly-parameter-types.mdx +0 -408
  134. package/docs/rules/prefer-readonly.mdx +0 -111
  135. package/docs/rules/prefer-reduce-type-parameter.mdx +0 -66
  136. package/docs/rules/prefer-regexp-exec.mdx +0 -52
  137. package/docs/rules/prefer-return-this-type.mdx +0 -93
  138. package/docs/rules/prefer-string-starts-ends-with.mdx +0 -84
  139. package/docs/rules/prefer-ts-expect-error.mdx +0 -86
  140. package/docs/rules/promise-function-async.mdx +0 -143
  141. package/docs/rules/quotes.md +0 -15
  142. package/docs/rules/related-getter-setter-pairs.mdx +0 -61
  143. package/docs/rules/require-array-sort-compare.mdx +0 -89
  144. package/docs/rules/require-await.mdx +0 -53
  145. package/docs/rules/restrict-plus-operands.mdx +0 -245
  146. package/docs/rules/restrict-template-expressions.mdx +0 -167
  147. package/docs/rules/return-await.mdx +0 -339
  148. package/docs/rules/semi.md +0 -15
  149. package/docs/rules/sort-type-constituents.mdx +0 -209
  150. package/docs/rules/sort-type-union-intersection-members.mdx +0 -16
  151. package/docs/rules/space-before-blocks.md +0 -15
  152. package/docs/rules/space-before-function-paren.md +0 -15
  153. package/docs/rules/space-infix-ops.md +0 -15
  154. package/docs/rules/strict-boolean-expressions.mdx +0 -184
  155. package/docs/rules/switch-exhaustiveness-check.mdx +0 -280
  156. package/docs/rules/triple-slash-reference.mdx +0 -129
  157. package/docs/rules/type-annotation-spacing.md +0 -15
  158. package/docs/rules/typedef.mdx +0 -350
  159. package/docs/rules/unbound-method.mdx +0 -114
  160. package/docs/rules/unified-signatures.mdx +0 -132
  161. package/docs/rules/use-unknown-in-catch-callback-variable.mdx +0 -97
@@ -1,89 +0,0 @@
1
- ---
2
- description: 'Disallow duplicate constituents of union or intersection types.'
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-duplicate-type-constituents** for documentation.
11
-
12
- TypeScript supports types ("constituents") within union and intersection types being duplicates of each other.
13
- However, developers typically expect each constituent to be unique within its intersection or union.
14
- Duplicate values make the code overly verbose and generally reduce readability.
15
-
16
- This rule disallows duplicate union or intersection constituents.
17
- We consider types to be duplicate if they evaluate to the same result in the type system.
18
- For example, given `type A = string` and `type T = string | A`, this rule would flag that `A` is the same type as `string`.
19
-
20
- This rule also disallows explicitly listing `undefined` in a type union when a function parameter is marked as optional.
21
- Doing so is unnecessary.
22
- Please note that this check only applies to parameters, not properties.
23
- Therefore, it does not conflict with the [`exactOptionalPropertyTypes`](https://www.typescriptlang.org/tsconfig/#exactOptionalPropertyTypes) TypeScript compiler setting.
24
-
25
- <Tabs>
26
- <TabItem value="❌ Incorrect">
27
-
28
- ```ts
29
- type T1 = 'A' | 'A';
30
-
31
- type T2 = string | string | number;
32
-
33
- type T3 = { a: string } & { a: string };
34
-
35
- type T4 = [1, 2, 3] | [1, 2, 3];
36
-
37
- type StringA = string;
38
- type StringB = string;
39
- type T5 = StringA | StringB;
40
-
41
- const fn = (a?: string | undefined) => {};
42
- ```
43
-
44
- </TabItem>
45
- <TabItem value="✅ Correct">
46
-
47
- ```ts
48
- type T1 = 'A' | 'B';
49
-
50
- type T2 = string | number | boolean;
51
-
52
- type T3 = { a: string } & { b: string };
53
-
54
- type T4 = [1, 2, 3] | [1, 2, 3, 4];
55
-
56
- type StringA = string;
57
- type NumberB = number;
58
- type T5 = StringA | NumberB;
59
-
60
- const fn = (a?: string) => {};
61
- ```
62
-
63
- </TabItem>
64
- </Tabs>
65
-
66
- ## Options
67
-
68
- ### `ignoreIntersections`
69
-
70
- {/* insert option description */}
71
-
72
- When set to true, duplicate checks on intersection type constituents are ignored.
73
-
74
- ### `ignoreUnions`
75
-
76
- {/* insert option description */}
77
-
78
- When set to true, duplicate checks on union type constituents are ignored.
79
-
80
- ## When Not To Use It
81
-
82
- It can sometimes be useful for the sake of documentation to include aliases for the same type.
83
- 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.
84
-
85
- > In some of those cases, [branded types](https://basarat.gitbook.io/typescript/main-1/nominaltyping#using-interfaces) might be a type-safe way to represent the underlying data types.
86
-
87
- ## Related To
88
-
89
- - [no-redundant-type-constituents](./no-redundant-type-constituents.mdx)
@@ -1,64 +0,0 @@
1
- ---
2
- description: 'Disallow using the `delete` operator on computed key 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-dynamic-delete** for documentation.
11
-
12
- Deleting dynamically computed keys can be dangerous and in some cases not well optimized.
13
- Using the `delete` operator on keys that aren't runtime constants could be a sign that you're using the wrong data structures.
14
- Consider using a `Map` or `Set` if you’re using an object as a key-value collection.
15
-
16
- Dynamically adding and removing keys from objects can cause occasional edge case bugs. For example, some objects use "hidden properties" (such as `__data`) for private storage, and deleting them can break the object's internal state. Furthermore, [`delete`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete) cannot remove inherited properties or non-configurable properties. This makes it interact badly with anything more complicated than a plain object:
17
-
18
- - The [`length`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length) of an array is non-configurable, and deleting it is a runtime error.
19
- - You can't remove properties on the prototype of an object, such as deleting methods from class instances.
20
- - Sometimes, `delete` only removes the own property, leaving the inherited property intact. For example, deleting the [`name`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name) property of a function only removes the own property, but there's also a `Function.prototype.name` property that remains.
21
-
22
- ## Examples
23
-
24
- <Tabs>
25
- <TabItem value="❌ Incorrect">
26
-
27
- ```ts
28
- // Dynamic, difficult-to-reason-about lookups
29
- const name = 'name';
30
- delete container[name];
31
- delete container[name.toUpperCase()];
32
- ```
33
-
34
- </TabItem>
35
- <TabItem value="✅ Correct">
36
-
37
- ```ts
38
- const container: { [i: string]: number } = {
39
- /* ... */
40
- };
41
-
42
- // Constant runtime lookups by string index
43
- delete container.aaa;
44
-
45
- // Constants that must be accessed by []
46
- delete container[7];
47
- delete container[-1];
48
-
49
- // All strings are allowed, to be compatible with the noPropertyAccessFromIndexSignature
50
- // TS compiler option
51
- delete container['aaa'];
52
- delete container['Infinity'];
53
- ```
54
-
55
- </TabItem>
56
- </Tabs>
57
-
58
- ## When Not To Use It
59
-
60
- When you know your keys are safe to delete, this rule can be unnecessary.
61
- 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.
62
-
63
- Do not consider this rule as performance advice before profiling your code's bottlenecks.
64
- Even repeated minor performance slowdowns likely do not significantly affect your application's general perceived speed.
@@ -1,94 +0,0 @@
1
- ---
2
- description: 'Disallow empty functions.'
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-empty-function** for documentation.
11
-
12
- It adds support for handling TypeScript specific code that would otherwise trigger the rule.
13
-
14
- One example of valid TypeScript specific code that would otherwise trigger the `no-empty-function` rule is the use of [parameter properties](https://www.typescriptlang.org/docs/handbook/classes.html#parameter-properties) in constructor functions.
15
-
16
- ## Options
17
-
18
- This rule adds the following options:
19
-
20
- ```ts
21
- type AdditionalAllowOptionEntries =
22
- | 'private-constructors'
23
- | 'protected-constructors'
24
- | 'decoratedFunctions'
25
- | 'overrideMethods';
26
-
27
- type AllowOptionEntries =
28
- | BaseNoEmptyFunctionAllowOptionEntries
29
- | AdditionalAllowOptionEntries;
30
-
31
- interface Options extends BaseNoEmptyFunctionOptions {
32
- allow?: Array<AllowOptionEntries>;
33
- }
34
- const defaultOptions: Options = {
35
- ...baseNoEmptyFunctionDefaultOptions,
36
- allow: [],
37
- };
38
- ```
39
-
40
- ### allow: `private-constructors`
41
-
42
- Examples of correct code for the `{ "allow": ["private-constructors"] }` option:
43
-
44
- ```ts option='{ "allow": ["private-constructors"] }' showPlaygroundButton
45
- class Foo {
46
- private constructor() {}
47
- }
48
- ```
49
-
50
- ### allow: `protected-constructors`
51
-
52
- Examples of correct code for the `{ "allow": ["protected-constructors"] }` option:
53
-
54
- ```ts option='{ "allow": ["protected-constructors"] }' showPlaygroundButton
55
- class Foo {
56
- protected constructor() {}
57
- }
58
- ```
59
-
60
- ### allow: `decoratedFunctions`
61
-
62
- Examples of correct code for the `{ "allow": ["decoratedFunctions"] }` option:
63
-
64
- ```ts option='{ "allow": ["decoratedFunctions"] }' showPlaygroundButton
65
- class Foo {
66
- @decorator()
67
- foo() {}
68
- }
69
- ```
70
-
71
- ### allow: `overrideMethods`
72
-
73
- Examples of correct code for the `{ "allow": ["overrideMethods"] }` option:
74
-
75
- ```ts option='{ "allow": ["overrideMethods"] }' showPlaygroundButton
76
- abstract class Base {
77
- protected greet(): void {
78
- console.log('Hello!');
79
- }
80
- }
81
-
82
- class Foo extends Base {
83
- protected override greet(): void {}
84
- }
85
- ```
86
-
87
- ## When Not To Use It
88
-
89
- If you are working with external APIs that require functions even if they do nothing, then you may want to avoid this rule.
90
- 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.
91
-
92
- Test code often violates this rule as well.
93
- If your testing setup doesn't support "mock" or "spy" functions such as [`jest.fn()`](https://jestjs.io/docs/mock-functions), [`sinon.spy()`](https://sinonjs.org/releases/latest/spies), or [`vi.fn()`](https://vitest.dev/guide/mocking.html), you may wish to disable this rule in test files.
94
- Again, if those cases aren't extremely common, 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 in test files.
@@ -1,75 +0,0 @@
1
- ---
2
- description: 'Disallow the declaration of empty interfaces.'
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-empty-interface** for documentation.
11
-
12
- :::danger Deprecated
13
-
14
- This rule has been deprecated in favour of the more comprehensive [`@typescript-eslint/no-empty-object-type`](./no-empty-object-type.mdx) rule.
15
-
16
- :::
17
-
18
- An empty interface in TypeScript does very little: any non-nullable value is assignable to `{}`.
19
- Using an empty interface is often a sign of programmer error, such as misunderstanding the concept of `{}` or forgetting to fill in fields.
20
-
21
- This rule aims to ensure that only meaningful interfaces are declared in the code.
22
-
23
- ## Examples
24
-
25
- <Tabs>
26
- <TabItem value="❌ Incorrect">
27
-
28
- ```ts
29
- // an empty interface
30
- interface Foo {}
31
-
32
- // an interface with only one supertype (Bar === Foo)
33
- interface Bar extends Foo {}
34
-
35
- // an interface with an empty list of supertypes
36
- interface Baz {}
37
- ```
38
-
39
- </TabItem>
40
- <TabItem value="✅ Correct">
41
-
42
- ```ts
43
- // an interface with any number of members
44
- interface Foo {
45
- name: string;
46
- }
47
-
48
- // same as above
49
- interface Bar {
50
- age: number;
51
- }
52
-
53
- // an interface with more than one supertype
54
- // in this case the interface can be used as a replacement of an intersection type.
55
- interface Baz extends Foo, Bar {}
56
- ```
57
-
58
- </TabItem>
59
- </Tabs>
60
-
61
- ## Options
62
-
63
- ### `allowSingleExtends`
64
-
65
- {/* insert option description */}
66
-
67
- `allowSingleExtends: true` will silence warnings about extending a single interface without adding additional members.
68
-
69
- ## When Not To Use It
70
-
71
- If you don't care about having empty/meaningless interfaces, then you will not need this rule.
72
-
73
- ## Related To
74
-
75
- - [`no-empty-object-type`](./no-empty-object-type.mdx)
@@ -1,150 +0,0 @@
1
- ---
2
- description: 'Disallow accidentally using the "empty object" type.'
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-empty-object-type** for documentation.
11
-
12
- The `{}`, or "empty object" type in TypeScript is a common source of confusion for developers unfamiliar with TypeScript's structural typing.
13
- `{}` represents any _non-nullish value_, including literals like `0` and `""`:
14
-
15
- ```ts
16
- let anyNonNullishValue: {} = 'Intentionally allowed by TypeScript.';
17
- ```
18
-
19
- Often, developers writing `{}` actually mean either:
20
-
21
- - `object`: representing any _object_ value
22
- - `unknown`: representing any value at all, including `null` and `undefined`
23
-
24
- In other words, the "empty object" type `{}` really means _"any value that is defined"_.
25
- That includes arrays, class instances, functions, and primitives such as `string` and `symbol`.
26
-
27
- To avoid confusion around the `{}` type allowing any _non-nullish value_, this rule bans usage of the `{}` type.
28
- That includes interfaces and object type aliases with no fields.
29
-
30
- :::tip
31
- If you do have a use case for an API allowing `{}`, you can always configure the [rule's options](#options), use an [ESLint disable comment](https://eslint.org/docs/latest/use/configure/rules#using-configuration-comments-1), or [disable the rule in your ESLint config](https://eslint.org/docs/latest/use/configure/rules#using-configuration-files-1).
32
- :::
33
-
34
- Note that this rule does not report on:
35
-
36
- - `{}` as a type constituent in an intersection type (e.g. types like TypeScript's built-in `type NonNullable<T> = T & {}`), as this can be useful in type system operations.
37
- - Interfaces that extend from multiple other interfaces.
38
-
39
- ## Examples
40
-
41
- <Tabs>
42
- <TabItem value="❌ Incorrect">
43
-
44
- ```ts
45
- let anyObject: {};
46
- let anyValue: {};
47
-
48
- interface AnyObjectA {}
49
- interface AnyValueA {}
50
-
51
- type AnyObjectB = {};
52
- type AnyValueB = {};
53
- ```
54
-
55
- </TabItem>
56
- <TabItem value="✅ Correct">
57
-
58
- ```ts
59
- let anyObject: object;
60
- let anyValue: unknown;
61
-
62
- type AnyObjectA = object;
63
- type AnyValueA = unknown;
64
-
65
- type AnyObjectB = object;
66
- type AnyValueB = unknown;
67
-
68
- let objectWith: { property: boolean };
69
-
70
- interface InterfaceWith {
71
- property: boolean;
72
- }
73
-
74
- type TypeWith = { property: boolean };
75
- ```
76
-
77
- </TabItem>
78
- </Tabs>
79
-
80
- ## Options
81
-
82
- By default, this rule flags both interfaces and object types.
83
-
84
- ### `allowInterfaces`
85
-
86
- {/* insert option description */}
87
-
88
- Allowed values are:
89
-
90
- - `'always'`: to always allow interfaces with no fields
91
- - `'never'` _(default)_: to never allow interfaces with no fields
92
- - `'with-single-extends'`: to allow empty interfaces that `extend` from a single base interface
93
-
94
- Examples of **correct** code for this rule with `{ allowInterfaces: 'with-single-extends' }`:
95
-
96
- ```ts option='{ "allowInterfaces": "with-single-extends" }' showPlaygroundButton
97
- interface Base {
98
- value: boolean;
99
- }
100
-
101
- interface Derived extends Base {}
102
- ```
103
-
104
- ### `allowObjectTypes`
105
-
106
- {/* insert option description */}
107
-
108
- Allowed values are:
109
-
110
- - `'always'`: to always allow object type literals with no fields
111
- - `'never'` _(default)_: to never allow object type literals with no fields
112
-
113
- ### `allowWithName`
114
-
115
- {/* insert option description */}
116
-
117
- This can be useful if your existing code style includes a pattern of declaring empty types with `{}` instead of `object`.
118
-
119
- Examples of code for this rule with `{ allowWithName: 'Props$' }`:
120
-
121
- <Tabs>
122
- <TabItem value="❌ Incorrect">
123
-
124
- ```ts option='{ "allowWithName": "Props$" }' showPlaygroundButton
125
- interface InterfaceValue {}
126
-
127
- type TypeValue = {};
128
- ```
129
-
130
- </TabItem>
131
- <TabItem value="✅ Correct">
132
-
133
- ```ts option='{ "allowWithName": "Props$" }' showPlaygroundButton
134
- interface InterfaceProps {}
135
-
136
- type TypeProps = {};
137
- ```
138
-
139
- </TabItem>
140
- </Tabs>
141
-
142
- ## When Not To Use It
143
-
144
- If your code commonly needs to represent the _"any non-nullish value"_ type, this rule may not be for you.
145
- Projects that extensively use type operations such as conditional types and mapped types oftentimes benefit from disabling this rule.
146
-
147
- ## Further Reading
148
-
149
- - [Enhancement: [ban-types] Split the {} ban into a separate, better-phrased rule](https://github.com/typescript-eslint/typescript-eslint/issues/8700)
150
- - [The Empty Object Type in TypeScript](https://www.totaltypescript.com/the-empty-object-type-in-typescript)
@@ -1,177 +0,0 @@
1
- ---
2
- description: 'Disallow the `any` type.'
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-explicit-any** for documentation.
11
-
12
- The `any` type in TypeScript is a dangerous "escape hatch" from the type system.
13
- Using `any` disables many type checking rules and is generally best used only as a last resort or when prototyping code.
14
- This rule reports on explicit uses of the `any` keyword as a type annotation.
15
-
16
- Preferable alternatives to `any` include:
17
-
18
- - If the type is known, describing it in an `interface` or `type`
19
- - If the type is not known, using the safer `unknown` type
20
-
21
- > TypeScript's `--noImplicitAny` compiler option prevents an implied `any`, but doesn't prevent `any` from being explicitly used the way this rule does.
22
-
23
- ## Examples
24
-
25
- <Tabs>
26
- <TabItem value="❌ Incorrect">
27
-
28
- ```ts
29
- const age: any = 'seventeen';
30
- ```
31
-
32
- ```ts
33
- const ages: any[] = ['seventeen'];
34
- ```
35
-
36
- ```ts
37
- const ages: Array<any> = ['seventeen'];
38
- ```
39
-
40
- ```ts
41
- function greet(): any {}
42
- ```
43
-
44
- ```ts
45
- function greet(): any[] {}
46
- ```
47
-
48
- ```ts
49
- function greet(): Array<any> {}
50
- ```
51
-
52
- ```ts
53
- function greet(): Array<Array<any>> {}
54
- ```
55
-
56
- ```ts
57
- function greet(param: Array<any>): string {}
58
- ```
59
-
60
- ```ts
61
- function greet(param: Array<any>): Array<any> {}
62
- ```
63
-
64
- </TabItem>
65
- <TabItem value="✅ Correct">
66
-
67
- ```ts
68
- const age: number = 17;
69
- ```
70
-
71
- ```ts
72
- const ages: number[] = [17];
73
- ```
74
-
75
- ```ts
76
- const ages: Array<number> = [17];
77
- ```
78
-
79
- ```ts
80
- function greet(): string {}
81
- ```
82
-
83
- ```ts
84
- function greet(): string[] {}
85
- ```
86
-
87
- ```ts
88
- function greet(): Array<string> {}
89
- ```
90
-
91
- ```ts
92
- function greet(): Array<Array<string>> {}
93
- ```
94
-
95
- ```ts
96
- function greet(param: Array<string>): string {}
97
- ```
98
-
99
- ```ts
100
- function greet(param: Array<string>): Array<string> {}
101
- ```
102
-
103
- </TabItem>
104
- </Tabs>
105
-
106
- ## Options
107
-
108
- ### `fixToUnknown`
109
-
110
- {/* insert option description */}
111
-
112
- By default, this rule will not provide automatic ESLint _fixes_: only opt-in _suggestions_.
113
- Switching types to `unknown` is safer but is likely to cause additional type errors.
114
-
115
- Enabling `{ "fixToUnknown": true }` gives the rule an auto-fixer to replace `: any` with `: unknown`.
116
-
117
- ### `ignoreRestArgs`
118
-
119
- {/* insert option description */}
120
-
121
- The examples below are **incorrect** when `{ignoreRestArgs: false}`, but **correct** when `{ignoreRestArgs: true}`.
122
-
123
- ```ts option='{ "ignoreRestArgs": false }' showPlaygroundButton
124
- function foo1(...args: any[]): void {}
125
- function foo2(...args: readonly any[]): void {}
126
- function foo3(...args: Array<any>): void {}
127
- function foo4(...args: ReadonlyArray<any>): void {}
128
-
129
- declare function bar(...args: any[]): void;
130
-
131
- const baz = (...args: any[]) => {};
132
- const qux = function (...args: any[]) {};
133
-
134
- type Quux = (...args: any[]) => void;
135
- type Quuz = new (...args: any[]) => void;
136
-
137
- interface Grault {
138
- (...args: any[]): void;
139
- }
140
- interface Corge {
141
- new (...args: any[]): void;
142
- }
143
- interface Garply {
144
- f(...args: any[]): void;
145
- }
146
- ```
147
-
148
- ## When Not To Use It
149
-
150
- `any` is always a dangerous escape hatch.
151
- Whenever possible, it is always safer to avoid it.
152
- TypeScript's `unknown` is almost always preferable to `any`.
153
-
154
- However, there are occasional situations where it can be necessary to use `any`.
155
- Most commonly:
156
-
157
- - If your project isn't fully onboarded to TypeScript yet, `any` can be temporarily used in places where types aren't yet known or representable
158
- - If an external package doesn't yet have typings and you want to use `any` pending adding a `.d.ts` for it
159
- - You're working with particularly complex or nuanced code that can't yet be represented in the TypeScript type system
160
-
161
- 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.
162
-
163
- ## Related To
164
-
165
- - [Avoiding `any`s with Linting and TypeScript](/blog/avoiding-anys)
166
- - [`no-unsafe-argument`](./no-unsafe-argument.mdx)
167
- - [`no-unsafe-assignment`](./no-unsafe-assignment.mdx)
168
- - [`no-unsafe-call`](./no-unsafe-call.mdx)
169
- - [`no-unsafe-member-access`](./no-unsafe-member-access.mdx)
170
- - [`no-unsafe-return`](./no-unsafe-return.mdx)
171
-
172
- ## Further Reading
173
-
174
- - TypeScript [`any` type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#any)
175
- - TypeScript's [`unknown` type](https://www.typescriptlang.org/docs/handbook/2/functions.html#unknown)
176
- - TypeScript [`any` type documentation](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#any)
177
- - TypeScript [`unknown` type release notes](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-0.html#new-unknown-top-type)