@typescript-eslint/eslint-plugin 8.0.0-alpha.3 → 8.0.0-alpha.31

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 (247) hide show
  1. package/dist/configs/all.js +5 -6
  2. package/dist/configs/all.js.map +1 -1
  3. package/dist/configs/disable-type-checked.js +1 -2
  4. package/dist/configs/disable-type-checked.js.map +1 -1
  5. package/dist/configs/recommended-type-checked-only.js +6 -0
  6. package/dist/configs/recommended-type-checked-only.js.map +1 -1
  7. package/dist/configs/recommended-type-checked.js +13 -2
  8. package/dist/configs/recommended-type-checked.js.map +1 -1
  9. package/dist/configs/recommended.js +7 -2
  10. package/dist/configs/recommended.js.map +1 -1
  11. package/dist/configs/strict-type-checked-only.js +2 -2
  12. package/dist/configs/strict-type-checked-only.js.map +1 -1
  13. package/dist/configs/strict-type-checked.js +9 -5
  14. package/dist/configs/strict-type-checked.js.map +1 -1
  15. package/dist/configs/strict.js +7 -3
  16. package/dist/configs/strict.js.map +1 -1
  17. package/dist/configs/stylistic-type-checked-only.js +3 -0
  18. package/dist/configs/stylistic-type-checked-only.js.map +1 -1
  19. package/dist/configs/stylistic-type-checked.js +3 -2
  20. package/dist/configs/stylistic-type-checked.js.map +1 -1
  21. package/dist/configs/stylistic.js +0 -2
  22. package/dist/configs/stylistic.js.map +1 -1
  23. package/dist/rules/adjacent-overload-signatures.js +2 -6
  24. package/dist/rules/adjacent-overload-signatures.js.map +1 -1
  25. package/dist/rules/class-literal-property-style.js +9 -7
  26. package/dist/rules/class-literal-property-style.js.map +1 -1
  27. package/dist/rules/consistent-type-assertions.js +5 -19
  28. package/dist/rules/consistent-type-assertions.js.map +1 -1
  29. package/dist/rules/consistent-type-definitions.js +1 -1
  30. package/dist/rules/consistent-type-definitions.js.map +1 -1
  31. package/dist/rules/default-param-last.js +6 -1
  32. package/dist/rules/default-param-last.js.map +1 -1
  33. package/dist/rules/dot-notation.js +8 -1
  34. package/dist/rules/dot-notation.js.map +1 -1
  35. package/dist/rules/enum-utils/shared.js +3 -4
  36. package/dist/rules/enum-utils/shared.js.map +1 -1
  37. package/dist/rules/explicit-member-accessibility.js +49 -37
  38. package/dist/rules/explicit-member-accessibility.js.map +1 -1
  39. package/dist/rules/index.js +10 -6
  40. package/dist/rules/index.js.map +1 -1
  41. package/dist/rules/init-declarations.js +57 -1
  42. package/dist/rules/init-declarations.js.map +1 -1
  43. package/dist/rules/member-ordering.js +8 -2
  44. package/dist/rules/member-ordering.js.map +1 -1
  45. package/dist/rules/naming-convention-utils/parse-options.js +2 -5
  46. package/dist/rules/naming-convention-utils/parse-options.js.map +1 -1
  47. package/dist/rules/naming-convention-utils/shared.js +3 -4
  48. package/dist/rules/naming-convention-utils/shared.js.map +1 -1
  49. package/dist/rules/naming-convention-utils/validator.js +1 -2
  50. package/dist/rules/naming-convention-utils/validator.js.map +1 -1
  51. package/dist/rules/naming-convention.js +3 -1
  52. package/dist/rules/naming-convention.js.map +1 -1
  53. package/dist/rules/no-array-delete.js +1 -1
  54. package/dist/rules/no-array-delete.js.map +1 -1
  55. package/dist/rules/no-base-to-string.js +1 -1
  56. package/dist/rules/no-base-to-string.js.map +1 -1
  57. package/dist/rules/no-duplicate-type-constituents.js +1 -1
  58. package/dist/rules/no-duplicate-type-constituents.js.map +1 -1
  59. package/dist/rules/no-dynamic-delete.js +8 -40
  60. package/dist/rules/no-dynamic-delete.js.map +1 -1
  61. package/dist/rules/no-empty-interface.js +2 -1
  62. package/dist/rules/no-empty-interface.js.map +1 -1
  63. package/dist/rules/no-empty-object-type.js +141 -0
  64. package/dist/rules/no-empty-object-type.js.map +1 -0
  65. package/dist/rules/no-extraneous-class.js +11 -3
  66. package/dist/rules/no-extraneous-class.js.map +1 -1
  67. package/dist/rules/no-floating-promises.js +62 -39
  68. package/dist/rules/no-floating-promises.js.map +1 -1
  69. package/dist/rules/no-implied-eval.js +1 -8
  70. package/dist/rules/no-implied-eval.js.map +1 -1
  71. package/dist/rules/no-inferrable-types.js +1 -2
  72. package/dist/rules/no-inferrable-types.js.map +1 -1
  73. package/dist/rules/no-invalid-this.js +6 -0
  74. package/dist/rules/no-invalid-this.js.map +1 -1
  75. package/dist/rules/no-magic-numbers.js +32 -1
  76. package/dist/rules/no-magic-numbers.js.map +1 -1
  77. package/dist/rules/no-misused-new.js +4 -3
  78. package/dist/rules/no-misused-new.js.map +1 -1
  79. package/dist/rules/no-require-imports.js +13 -3
  80. package/dist/rules/no-require-imports.js.map +1 -1
  81. package/dist/rules/no-restricted-imports.js +3 -6
  82. package/dist/rules/no-restricted-imports.js.map +1 -1
  83. package/dist/rules/{ban-types.js → no-restricted-types.js} +17 -91
  84. package/dist/rules/no-restricted-types.js.map +1 -0
  85. package/dist/rules/no-shadow.js +1 -1
  86. package/dist/rules/no-shadow.js.map +1 -1
  87. package/dist/rules/{no-useless-template-literals.js → no-unnecessary-template-expression.js} +6 -6
  88. package/dist/rules/no-unnecessary-template-expression.js.map +1 -0
  89. package/dist/rules/no-unnecessary-type-assertion.js +15 -13
  90. package/dist/rules/no-unnecessary-type-assertion.js.map +1 -1
  91. package/dist/rules/no-unsafe-argument.js +4 -2
  92. package/dist/rules/no-unsafe-argument.js.map +1 -1
  93. package/dist/rules/no-unsafe-assignment.js +24 -9
  94. package/dist/rules/no-unsafe-assignment.js.map +1 -1
  95. package/dist/rules/no-unsafe-function-type.js +47 -0
  96. package/dist/rules/no-unsafe-function-type.js.map +1 -0
  97. package/dist/rules/no-unsafe-member-access.js +8 -2
  98. package/dist/rules/no-unsafe-member-access.js.map +1 -1
  99. package/dist/rules/no-unsafe-return.js +1 -1
  100. package/dist/rules/no-unsafe-return.js.map +1 -1
  101. package/dist/rules/no-unsafe-unary-minus.js +1 -0
  102. package/dist/rules/no-unsafe-unary-minus.js.map +1 -1
  103. package/dist/rules/no-unused-expressions.js +1 -0
  104. package/dist/rules/no-unused-expressions.js.map +1 -1
  105. package/dist/rules/no-unused-vars.js +191 -77
  106. package/dist/rules/no-unused-vars.js.map +1 -1
  107. package/dist/rules/no-useless-constructor.js +0 -1
  108. package/dist/rules/no-useless-constructor.js.map +1 -1
  109. package/dist/rules/no-var-requires.js +2 -1
  110. package/dist/rules/no-var-requires.js.map +1 -1
  111. package/dist/rules/no-wrapper-object-types.js +61 -0
  112. package/dist/rules/no-wrapper-object-types.js.map +1 -0
  113. package/dist/rules/only-throw-error.js +1 -1
  114. package/dist/rules/only-throw-error.js.map +1 -1
  115. package/dist/rules/prefer-as-const.js +1 -1
  116. package/dist/rules/prefer-as-const.js.map +1 -1
  117. package/dist/rules/prefer-destructuring.js +3 -1
  118. package/dist/rules/prefer-destructuring.js.map +1 -1
  119. package/dist/rules/prefer-find.js +1 -0
  120. package/dist/rules/prefer-find.js.map +1 -1
  121. package/dist/rules/prefer-includes.js +1 -1
  122. package/dist/rules/prefer-includes.js.map +1 -1
  123. package/dist/rules/prefer-literal-enum-member.js +43 -2
  124. package/dist/rules/prefer-literal-enum-member.js.map +1 -1
  125. package/dist/rules/prefer-namespace-keyword.js +1 -1
  126. package/dist/rules/prefer-namespace-keyword.js.map +1 -1
  127. package/dist/rules/prefer-nullish-coalescing.js +8 -6
  128. package/dist/rules/prefer-nullish-coalescing.js.map +1 -1
  129. package/dist/rules/prefer-optional-chain-utils/analyzeChain.js +1 -2
  130. package/dist/rules/prefer-optional-chain-utils/analyzeChain.js.map +1 -1
  131. package/dist/rules/prefer-optional-chain-utils/checkNullishAndReport.js +1 -2
  132. package/dist/rules/prefer-optional-chain-utils/checkNullishAndReport.js.map +1 -1
  133. package/dist/rules/prefer-optional-chain-utils/compareNodes.js +1 -2
  134. package/dist/rules/prefer-optional-chain-utils/compareNodes.js.map +1 -1
  135. package/dist/rules/prefer-optional-chain-utils/gatherLogicalOperands.js +6 -11
  136. package/dist/rules/prefer-optional-chain-utils/gatherLogicalOperands.js.map +1 -1
  137. package/dist/rules/prefer-promise-reject-errors.js +1 -1
  138. package/dist/rules/prefer-promise-reject-errors.js.map +1 -1
  139. package/dist/rules/prefer-readonly.js +24 -12
  140. package/dist/rules/prefer-readonly.js.map +1 -1
  141. package/dist/rules/prefer-regexp-exec.js +15 -6
  142. package/dist/rules/prefer-regexp-exec.js.map +1 -1
  143. package/dist/rules/prefer-string-starts-ends-with.js.map +1 -1
  144. package/dist/rules/prefer-ts-expect-error.js +2 -1
  145. package/dist/rules/prefer-ts-expect-error.js.map +1 -1
  146. package/dist/rules/require-await.js +1 -1
  147. package/dist/rules/require-await.js.map +1 -1
  148. package/dist/rules/restrict-template-expressions.js +2 -2
  149. package/dist/rules/restrict-template-expressions.js.map +1 -1
  150. package/dist/rules/return-await.js +117 -52
  151. package/dist/rules/return-await.js.map +1 -1
  152. package/dist/rules/sort-type-constituents.js +23 -1
  153. package/dist/rules/sort-type-constituents.js.map +1 -1
  154. package/dist/rules/strict-boolean-expressions.js +1 -3
  155. package/dist/rules/strict-boolean-expressions.js.map +1 -1
  156. package/dist/rules/switch-exhaustiveness-check.js +7 -9
  157. package/dist/rules/switch-exhaustiveness-check.js.map +1 -1
  158. package/dist/rules/unified-signatures.js +3 -1
  159. package/dist/rules/unified-signatures.js.map +1 -1
  160. package/dist/util/astUtils.js +2 -3
  161. package/dist/util/astUtils.js.map +1 -1
  162. package/dist/util/collectUnusedVariables.js +81 -64
  163. package/dist/util/collectUnusedVariables.js.map +1 -1
  164. package/dist/util/createRule.js.map +1 -1
  165. package/dist/util/escapeRegExp.js +1 -2
  166. package/dist/util/escapeRegExp.js.map +1 -1
  167. package/dist/util/explicitReturnTypeUtils.js +11 -7
  168. package/dist/util/explicitReturnTypeUtils.js.map +1 -1
  169. package/dist/util/getESLintCoreRule.js +2 -2
  170. package/dist/util/getESLintCoreRule.js.map +1 -1
  171. package/dist/util/getForStatementHeadLoc.js +1 -2
  172. package/dist/util/getForStatementHeadLoc.js.map +1 -1
  173. package/dist/util/getFunctionHeadLoc.js +1 -2
  174. package/dist/util/getFunctionHeadLoc.js.map +1 -1
  175. package/dist/util/getMemberHeadLoc.js +80 -0
  176. package/dist/util/getMemberHeadLoc.js.map +1 -0
  177. package/dist/util/getOperatorPrecedence.js +5 -5
  178. package/dist/util/getOperatorPrecedence.js.map +1 -1
  179. package/dist/util/getStaticStringValue.js +1 -2
  180. package/dist/util/getStaticStringValue.js.map +1 -1
  181. package/dist/util/getStringLength.js +1 -2
  182. package/dist/util/getStringLength.js.map +1 -1
  183. package/dist/util/getTextWithParentheses.js +17 -0
  184. package/dist/util/getTextWithParentheses.js.map +1 -0
  185. package/dist/util/getThisExpression.js +1 -2
  186. package/dist/util/getThisExpression.js.map +1 -1
  187. package/dist/util/getWrappedCode.js +1 -2
  188. package/dist/util/getWrappedCode.js.map +1 -1
  189. package/dist/util/getWrappingFixer.js +2 -3
  190. package/dist/util/getWrappingFixer.js.map +1 -1
  191. package/dist/util/index.js +2 -0
  192. package/dist/util/index.js.map +1 -1
  193. package/dist/util/isAssignee.js +1 -2
  194. package/dist/util/isAssignee.js.map +1 -1
  195. package/dist/util/isNodeEqual.js +1 -2
  196. package/dist/util/isNodeEqual.js.map +1 -1
  197. package/dist/util/isNullLiteral.js +1 -2
  198. package/dist/util/isNullLiteral.js.map +1 -1
  199. package/dist/util/isUndefinedIdentifier.js +1 -2
  200. package/dist/util/isUndefinedIdentifier.js.map +1 -1
  201. package/dist/util/misc.js +14 -14
  202. package/dist/util/misc.js.map +1 -1
  203. package/dist/util/objectIterators.js +3 -4
  204. package/dist/util/objectIterators.js.map +1 -1
  205. package/dist/util/scopeUtils.js +11 -0
  206. package/dist/util/scopeUtils.js.map +1 -0
  207. package/docs/rules/ban-types.md +22 -0
  208. package/docs/rules/consistent-indexed-object-style.mdx +1 -1
  209. package/docs/rules/consistent-type-imports.mdx +1 -1
  210. package/docs/rules/naming-convention.mdx +16 -0
  211. package/docs/rules/no-base-to-string.mdx +1 -1
  212. package/docs/rules/no-duplicate-imports.mdx +1 -1
  213. package/docs/rules/no-dynamic-delete.mdx +12 -7
  214. package/docs/rules/no-empty-interface.mdx +10 -0
  215. package/docs/rules/no-empty-object-type.mdx +145 -0
  216. package/docs/rules/no-extraneous-class.mdx +6 -0
  217. package/docs/rules/no-floating-promises.mdx +103 -4
  218. package/docs/rules/no-import-type-side-effects.mdx +1 -1
  219. package/docs/rules/no-misused-promises.mdx +18 -20
  220. package/docs/rules/no-require-imports.mdx +28 -1
  221. package/docs/rules/no-restricted-types.mdx +71 -0
  222. package/docs/rules/no-type-alias.mdx +1 -1
  223. package/docs/rules/no-unnecessary-boolean-literal-compare.mdx +12 -12
  224. package/docs/rules/no-unnecessary-template-expression.mdx +85 -0
  225. package/docs/rules/no-unsafe-function-type.mdx +63 -0
  226. package/docs/rules/no-unused-expressions.mdx +41 -1
  227. package/docs/rules/no-unused-vars.mdx +5 -0
  228. package/docs/rules/no-useless-template-literals.mdx +3 -59
  229. package/docs/rules/no-var-requires.mdx +6 -0
  230. package/docs/rules/no-wrapper-object-types.mdx +67 -0
  231. package/docs/rules/only-throw-error.mdx +7 -0
  232. package/docs/rules/prefer-readonly-parameter-types.mdx +10 -4
  233. package/docs/rules/prefer-ts-expect-error.mdx +10 -0
  234. package/docs/rules/require-await.mdx +2 -2
  235. package/docs/rules/restrict-template-expressions.mdx +1 -1
  236. package/docs/rules/return-await.mdx +55 -21
  237. package/docs/rules/sort-type-constituents.mdx +29 -9
  238. package/docs/rules/sort-type-union-intersection-members.mdx +12 -0
  239. package/docs/rules/unbound-method.mdx +3 -2
  240. package/package.json +14 -19
  241. package/rules.d.ts +6 -2
  242. package/dist/rules/ban-types.js.map +0 -1
  243. package/dist/rules/no-throw-literal.js +0 -97
  244. package/dist/rules/no-throw-literal.js.map +0 -1
  245. package/dist/rules/no-useless-template-literals.js.map +0 -1
  246. package/docs/rules/ban-types.mdx +0 -138
  247. package/docs/rules/no-throw-literal.mdx +0 -25
@@ -1,6 +1,6 @@
1
1
  :::danger Deprecated
2
2
 
3
- This rule has been deprecated in favour of the [`import/no-duplicates`](https://github.com/import-js/eslint-plugin-import/blob/HEAD/docs/rules/no-duplicates.mdx) rule.
3
+ This rule has been deprecated in favour of the [`import/no-duplicates`](https://github.com/import-js/eslint-plugin-import/blob/HEAD/docs/rules/no-duplicates.md) rule.
4
4
 
5
5
  :::
6
6
 
@@ -11,9 +11,13 @@ import TabItem from '@theme/TabItem';
11
11
 
12
12
  Deleting dynamically computed keys can be dangerous and in some cases not well optimized.
13
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
- Using `Object`s with added and removed keys can cause occasional edge case bugs, such as if a key is named `"hasOwnProperty"`.
14
+ Consider using a `Map` or `Set` if you’re using an object as a key-value collection.
15
15
 
16
- > Consider using a `Map` or `Set` if you’re storing collections of objects.
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.
17
21
 
18
22
  ## Examples
19
23
 
@@ -21,10 +25,6 @@ Using `Object`s with added and removed keys can cause occasional edge case bugs,
21
25
  <TabItem value="❌ Incorrect">
22
26
 
23
27
  ```ts
24
- // Can be replaced with the constant equivalents, such as container.aaa
25
- delete container['aaa'];
26
- delete container['Infinity'];
27
-
28
28
  // Dynamic, difficult-to-reason-about lookups
29
29
  const name = 'name';
30
30
  delete container[name];
@@ -44,7 +44,12 @@ delete container.aaa;
44
44
 
45
45
  // Constants that must be accessed by []
46
46
  delete container[7];
47
- delete container['-Infinity'];
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'];
48
53
  ```
49
54
 
50
55
  </TabItem>
@@ -9,6 +9,12 @@ import TabItem from '@theme/TabItem';
9
9
  >
10
10
  > See **https://typescript-eslint.io/rules/no-empty-interface** for documentation.
11
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
+
12
18
  An empty interface in TypeScript does very little: any non-nullable value is assignable to `{}`.
13
19
  Using an empty interface is often a sign of programmer error, such as misunderstanding the concept of `{}` or forgetting to fill in fields.
14
20
 
@@ -61,3 +67,7 @@ interface Baz extends Foo, Bar {}
61
67
  ## When Not To Use It
62
68
 
63
69
  If you don't care about having empty/meaningless interfaces, then you will not need this rule.
70
+
71
+ ## Related To
72
+
73
+ - [`no-empty-object-type`](./no-empty-object-type.mdx)
@@ -0,0 +1,145 @@
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
+ Whether to allow empty interfaces, as one of:
87
+
88
+ - `'always'`: to always allow interfaces with no fields
89
+ - `'never'` _(default)_: to never allow interfaces with no fields
90
+ - `'with-single-extends'`: to allow empty interfaces that `extend` from a single base interface
91
+
92
+ Examples of **correct** code for this rule with `{ allowInterfaces: 'with-single-extends' }`:
93
+
94
+ ```ts option='{ "allowInterfaces": "with-single-extends" }' showPlaygroundButton
95
+ interface Base {
96
+ value: boolean;
97
+ }
98
+
99
+ interface Derived extends Base {}
100
+ ```
101
+
102
+ ### `allowObjectTypes`
103
+
104
+ Whether to allow empty object type literals, as one of:
105
+
106
+ - `'always'`: to always allow object type literals with no fields
107
+ - `'never'` _(default)_: to never allow object type literals with no fields
108
+
109
+ ### `allowWithName`
110
+
111
+ A stringified regular expression to allow interfaces and object type aliases with the configured name.
112
+ This can be useful if your existing code style includes a pattern of declaring empty types with `{}` instead of `object`.
113
+
114
+ Examples of code for this rule with `{ allowWithName: 'Props$' }`:
115
+
116
+ <Tabs>
117
+ <TabItem value="❌ Incorrect">
118
+
119
+ ```ts option='{ "allowWithName": "Props$" }' showPlaygroundButton
120
+ interface InterfaceValue {}
121
+
122
+ type TypeValue = {};
123
+ ```
124
+
125
+ </TabItem>
126
+ <TabItem value="✅ Correct">
127
+
128
+ ```ts option='{ "allowWithName": "Props$" }' showPlaygroundButton
129
+ interface InterfaceProps {}
130
+
131
+ type TypeProps = {};
132
+ ```
133
+
134
+ </TabItem>
135
+ </Tabs>
136
+
137
+ ## When Not To Use It
138
+
139
+ If your code commonly needs to represent the _"any non-nullish value"_ type, this rule may not be for you.
140
+ Projects that extensively use type operations such as conditional types and mapped types oftentimes benefit from disabling this rule.
141
+
142
+ ## Further Reading
143
+
144
+ - [Enhancement: [ban-types] Split the {} ban into a separate, better-phrased rule](https://github.com/typescript-eslint/typescript-eslint/issues/8700)
145
+ - [The Empty Object Type in TypeScript](https://www.totaltypescript.com/the-empty-object-type-in-typescript)
@@ -42,6 +42,8 @@ class HelloWorldLogger {
42
42
  console.log('Hello, world!');
43
43
  }
44
44
  }
45
+
46
+ abstract class Foo {}
45
47
  ```
46
48
 
47
49
  </TabItem>
@@ -57,6 +59,10 @@ export function isProduction() {
57
59
  function logHelloWorld() {
58
60
  console.log('Hello, world!');
59
61
  }
62
+
63
+ abstract class Foo {
64
+ abstract prop: string;
65
+ }
60
66
  ```
61
67
 
62
68
  </TabItem>
@@ -17,13 +17,14 @@ Valid ways of handling a Promise-valued statement include:
17
17
 
18
18
  - `await`ing it
19
19
  - `return`ing it
20
+ - `void`ing it
20
21
  - Calling its `.then()` with two arguments
21
22
  - Calling its `.catch()` with one argument
22
23
 
23
24
  This rule also reports when an Array containing Promises is created and not properly handled. The main way to resolve this is by using one of the Promise concurrency methods to create a single Promise, then handling that according to the procedure above. These methods include:
24
25
 
25
- - `Promise.all()`,
26
- - `Promise.allSettled()`,
26
+ - `Promise.all()`
27
+ - `Promise.allSettled()`
27
28
  - `Promise.any()`
28
29
  - `Promise.race()`
29
30
 
@@ -63,6 +64,9 @@ await promise;
63
64
  async function returnsPromise() {
64
65
  return 'value';
65
66
  }
67
+
68
+ void returnsPromise();
69
+
66
70
  returnsPromise().then(
67
71
  () => {},
68
72
  () => {},
@@ -82,7 +86,7 @@ await Promise.all([1, 2, 3].map(async x => x + 1));
82
86
 
83
87
  ### `ignoreVoid`
84
88
 
85
- This allows you to stop the rule reporting promises consumed with void operator.
89
+ This option, which is `true` by default, allows you to stop the rule reporting promises consumed with void operator.
86
90
  This can be a good way to explicitly mark a promise as intentionally not awaited.
87
91
 
88
92
  Examples of **correct** code for this rule with `{ ignoreVoid: true }`:
@@ -100,7 +104,7 @@ With this option set to `true`, and if you are using `no-void`, you should turn
100
104
 
101
105
  ### `ignoreIIFE`
102
106
 
103
- This allows you to skip checking of async IIFEs (Immediately Invoked function Expressions).
107
+ This allows you to skip checking of async IIFEs (Immediately Invoked Function Expressions).
104
108
 
105
109
  Examples of **correct** code for this rule with `{ ignoreIIFE: true }`:
106
110
 
@@ -115,6 +119,101 @@ await (async function () {
115
119
  })();
116
120
  ```
117
121
 
122
+ ### `allowForKnownSafePromises`
123
+
124
+ This option allows marking specific types as "safe" to be floating. For example, you may need to do this in the case of libraries whose APIs return Promises whose rejections are safely handled by the library.
125
+
126
+ This option takes an array of type specifiers to consider safe.
127
+ Each item in the array must have one of the following forms:
128
+
129
+ - A type defined in a file (`{ from: "file", name: "Foo", path: "src/foo-file.ts" }` with `path` being an optional path relative to the project root directory)
130
+ - A type from the default library (`{ from: "lib", name: "PromiseLike" }`)
131
+ - A type from a package (`{ from: "package", name: "Foo", package: "foo-lib" }`, this also works for types defined in a typings package).
132
+
133
+ Examples of code for this rule with:
134
+
135
+ ```json
136
+ {
137
+ "allowForKnownSafePromises": [
138
+ { "from": "file", "name": "SafePromise" },
139
+ { "from": "lib", "name": "PromiseLike" },
140
+ { "from": "package", "name": "Bar", "package": "bar-lib" }
141
+ ]
142
+ }
143
+ ```
144
+
145
+ <Tabs>
146
+ <TabItem value="❌ Incorrect">
147
+
148
+ ```ts option='{"allowForKnownSafePromises":[{"from":"file","name":"SafePromise"},{"from":"lib","name":"PromiseLike"},{"from":"package","name":"Bar","package":"bar-lib"}]}'
149
+ let promise: Promise<number> = Promise.resolve(2);
150
+ promise;
151
+
152
+ function returnsPromise(): Promise<number> {
153
+ return Promise.resolve(42);
154
+ }
155
+
156
+ returnsPromise();
157
+ ```
158
+
159
+ </TabItem>
160
+ <TabItem value="✅ Correct">
161
+
162
+ ```ts option='{"allowForKnownSafePromises":[{"from":"file","name":"SafePromise"},{"from":"lib","name":"PromiseLike"},{"from":"package","name":"Bar","package":"bar-lib"}]}'
163
+ // promises can be marked as safe by using branded types
164
+ type SafePromise = Promise<number> & { __linterBrands?: string };
165
+
166
+ let promise: SafePromise = Promise.resolve(2);
167
+ promise;
168
+
169
+ function returnsSafePromise(): SafePromise {
170
+ return Promise.resolve(42);
171
+ }
172
+
173
+ returnsSafePromise();
174
+ ```
175
+
176
+ </TabItem>
177
+ </Tabs>
178
+
179
+ ### `allowForKnownSafeCalls`
180
+
181
+ This option allows marking specific functions as "safe" to be called to create floating Promises.
182
+ For example, you may need to do this in the case of libraries whose APIs may be called without handling the resultant Promises.
183
+
184
+ This option takes the same array format as [`allowForKnownSafePromises`](#allowForKnownSafePromises).
185
+
186
+ Examples of code for this rule with:
187
+
188
+ ```json
189
+ {
190
+ "allowForKnownSafeCalls": [
191
+ { "from": "file", "name": "safe", "path": "input.ts" }
192
+ ]
193
+ }
194
+ ```
195
+
196
+ <Tabs>
197
+ <TabItem value="❌ Incorrect">
198
+
199
+ ```ts option='{"allowForKnownSafeCalls":[{"from":"file","name":"safe","path":"input.ts"}]}'
200
+ declare function unsafe(...args: unknown[]): Promise<void>;
201
+
202
+ unsafe('...', () => {});
203
+ ```
204
+
205
+ </TabItem>
206
+ <TabItem value="✅ Correct">
207
+
208
+ ```ts option='{"allowForKnownSafeCalls":[{"from":"file","name":"safe","path":"input.ts"}]}' skipValidation
209
+ declare function safe(...args: unknown[]): Promise<void>;
210
+
211
+ safe('...', () => {});
212
+ ```
213
+
214
+ </TabItem>
215
+ </Tabs>
216
+
118
217
  ## When Not To Use It
119
218
 
120
219
  This rule can be difficult to enable on large existing projects that set up many floating Promises.
@@ -76,5 +76,5 @@ If you're not using TypeScript 5.0's `verbatimModuleSyntax` option and your proj
76
76
  ## Related To
77
77
 
78
78
  - [`consistent-type-imports`](./consistent-type-imports.mdx)
79
- - [`import/consistent-type-specifier-style`](https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/consistent-type-specifier-style.mdx)
79
+ - [`import/consistent-type-specifier-style`](https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/consistent-type-specifier-style.md)
80
80
  - [`import/no-duplicates` with `{"prefer-inline": true}`](https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-duplicates.md#inline-type-imports)
@@ -127,19 +127,18 @@ Examples of code for this rule with `checksVoidReturn: true`:
127
127
 
128
128
  ```ts option='{ "checksVoidReturn": true }'
129
129
  [1, 2, 3].forEach(async value => {
130
- await doSomething(value);
130
+ await fetch(`/${value}`);
131
131
  });
132
132
 
133
- new Promise(async (resolve, reject) => {
134
- await doSomething();
133
+ new Promise<void>(async (resolve, reject) => {
134
+ await fetch('/');
135
135
  resolve();
136
136
  });
137
137
 
138
- const eventEmitter = new EventEmitter();
139
- eventEmitter.on('some-event', async () => {
140
- synchronousCall();
141
- await doSomething();
142
- otherSynchronousCall();
138
+ document.addEventListener('click', async () => {
139
+ console.log('synchronous call');
140
+ await fetch('/');
141
+ console.log('synchronous call');
143
142
  });
144
143
  ```
145
144
 
@@ -169,8 +168,7 @@ new Promise((resolve, reject) => {
169
168
  });
170
169
 
171
170
  // Name the async wrapper to call it later
172
- const eventEmitter = new EventEmitter();
173
- eventEmitter.on('some-event', () => {
171
+ document.addEventListener('click', () => {
174
172
  const handler = async () => {
175
173
  await doSomething();
176
174
  otherSynchronousCall();
@@ -210,30 +208,30 @@ Examples of code for this rule with `checksSpreads: true`:
210
208
  <TabItem value="❌ Incorrect">
211
209
 
212
210
  ```ts option='{ "checksSpreads": true }'
213
- const getData = () => someAsyncOperation({ myArg: 'foo' });
211
+ const getData = () => fetch('/');
214
212
 
215
- return { foo: 42, ...getData() };
213
+ console.log({ foo: 42, ...getData() });
216
214
 
217
- const getData2 = async () => {
218
- await someAsyncOperation({ myArg: 'foo' });
215
+ const awaitData = async () => {
216
+ await fetch('/');
219
217
  };
220
218
 
221
- return { foo: 42, ...getData2() };
219
+ console.log({ foo: 42, ...awaitData() });
222
220
  ```
223
221
 
224
222
  </TabItem>
225
223
  <TabItem value="✅ Correct">
226
224
 
227
225
  ```ts option='{ "checksSpreads": true }'
228
- const getData = () => someAsyncOperation({ myArg: 'foo' });
226
+ const getData = () => fetch('/');
229
227
 
230
- return { foo: 42, ...(await getData()) };
228
+ console.log({ foo: 42, ...(await getData()) });
231
229
 
232
- const getData2 = async () => {
233
- await someAsyncOperation({ myArg: 'foo' });
230
+ const awaitData = async () => {
231
+ await fetch('/');
234
232
  };
235
233
 
236
- return { foo: 42, ...(await getData2()) };
234
+ console.log({ foo: 42, ...(await awaitData()) });
237
235
  ```
238
236
 
239
237
  </TabItem>
@@ -38,7 +38,7 @@ import * as lib3 from 'lib3';
38
38
 
39
39
  ### `allow`
40
40
 
41
- A array of strings. These strings will be compiled into regular expressions with the `u` flag and be used to test against the imported path. A common use case is to allow importing `package.json`. This is because `package.json` commonly lives outside of the TS root directory, so statically importing it would lead to root directory conflicts, especially with `resolveJsonModule` enabled. You can also use it to allow importing any JSON if your environment doesn't support JSON modules, or use it for other cases where `import` statements cannot work.
41
+ An array of strings. These strings will be compiled into regular expressions with the `u` flag and be used to test against the imported path. A common use case is to allow importing `package.json`. This is because `package.json` commonly lives outside of the TS root directory, so statically importing it would lead to root directory conflicts, especially with `resolveJsonModule` enabled. You can also use it to allow importing any JSON if your environment doesn't support JSON modules, or use it for other cases where `import` statements cannot work.
42
42
 
43
43
  With `{allow: ['/package\\.json$']}`:
44
44
 
@@ -59,6 +59,33 @@ console.log(require('../package.json').version);
59
59
  </TabItem>
60
60
  </Tabs>
61
61
 
62
+ ### `allowAsImport`
63
+
64
+ When set to `true`, the `import x = require(...)` declaration won't be reported.
65
+ This is useful if you use certain module options that require strict CommonJS interop semantics.
66
+
67
+ With `{allowAsImport: true}`:
68
+
69
+ <Tabs>
70
+ <TabItem value="❌ Incorrect">
71
+
72
+ ```ts option='{ "allowAsImport": true }'
73
+ var foo = require('foo');
74
+ const foo = require('foo');
75
+ let foo = require('foo');
76
+ ```
77
+
78
+ </TabItem>
79
+ <TabItem value="✅ Correct">
80
+
81
+ ```ts option='{ "allowAsImport": true }'
82
+ import foo = require('foo');
83
+ import foo from 'foo';
84
+ ```
85
+
86
+ </TabItem>
87
+ </Tabs>
88
+
62
89
  ## When Not To Use It
63
90
 
64
91
  If your project frequently uses older CommonJS `require`s, then this rule might not be applicable to you.
@@ -0,0 +1,71 @@
1
+ ---
2
+ description: 'Disallow certain 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-restricted-types** for documentation.
11
+
12
+ It can sometimes be useful to ban specific types from being used in type annotations.
13
+ For example, a project might be migrating from using one type to another, and want to ban references to the old type.
14
+
15
+ This rule can be configured to ban a list of specific types and can suggest alternatives.
16
+ Note that it does not ban the corresponding runtime objects from being used.
17
+
18
+ ## Options
19
+
20
+ ### `types`
21
+
22
+ An object whose keys are the types you want to ban, and the values are error messages.
23
+
24
+ The type can either be a type name literal (`OldType`) or a a type name with generic parameter instantiation(s) (`OldType<MyArgument>`).
25
+
26
+ The values can be:
27
+
28
+ - A string, which is the error message to be reported; or
29
+ - `false` to specifically un-ban this type (useful when you are using `extendDefaults`); or
30
+ - An object with the following properties:
31
+ - `message: string`: the message to display when the type is matched.
32
+ - `fixWith?: string`: a string to replace the banned type with when the fixer is run. If this is omitted, no fix will be done.
33
+ - `suggest?: string[]`: a list of suggested replacements for the banned type.
34
+
35
+ Example configuration:
36
+
37
+ ```jsonc
38
+ {
39
+ "@typescript-eslint/no-restricted-types": [
40
+ "error",
41
+ {
42
+ "types": {
43
+ // add a custom message to help explain why not to use it
44
+ "OldType": "Don't use OldType because it is unsafe",
45
+
46
+ // add a custom message, and tell the plugin how to fix it
47
+ "OldAPI": {
48
+ "message": "Use NewAPI instead",
49
+ "fixWith": "NewAPI",
50
+ },
51
+
52
+ // add a custom message, and tell the plugin how to suggest a fix
53
+ "SoonToBeOldAPI": {
54
+ "message": "Use NewAPI instead",
55
+ "suggest": ["NewAPIOne", "NewAPITwo"],
56
+ },
57
+ },
58
+ },
59
+ ],
60
+ }
61
+ ```
62
+
63
+ ## When Not To Use It
64
+
65
+ If you have no need to ban specific types from being used in type annotations, you don't need this rule.
66
+
67
+ ## Related To
68
+
69
+ - [`no-empty-object-type`](./no-empty-object-type.mdx)
70
+ - [`no-unsafe-function-type`](./no-unsafe-function-type.mdx)
71
+ - [`no-wrapper-object-types`](./no-wrapper-object-types.mdx)
@@ -19,7 +19,7 @@ TypeScript type aliases are a commonly necessary language feature; banning it al
19
19
  :::note
20
20
 
21
21
  If you want to ban certain classifications of type aliases, consider using [`no-restricted-syntax`](https://eslint.org/docs/latest/rules/no-restricted-syntax).
22
- See [Troubleshooting & FAQs](/troubleshooting#how-can-i-ban-specific-language-feature).
22
+ See [Troubleshooting & FAQs](/troubleshooting/faqs/general#how-can-i-ban-specific-language-feature).
23
23
 
24
24
  :::
25
25
 
@@ -114,11 +114,11 @@ if (someNullCondition !== false) {
114
114
 
115
115
  ```ts option='{ "allowComparingNullableBooleansToFalse": false }'
116
116
  declare const someUndefinedCondition: boolean | undefined;
117
- if (someUndefinedCondition ?? true) {
117
+ if (!(someUndefinedCondition ?? true)) {
118
118
  }
119
119
 
120
120
  declare const someNullCondition: boolean | null;
121
- if (!(someNullCondition ?? true)) {
121
+ if (someNullCondition ?? true) {
122
122
  }
123
123
  ```
124
124
 
@@ -127,16 +127,16 @@ if (!(someNullCondition ?? true)) {
127
127
 
128
128
  ## Fixer
129
129
 
130
- | Comparison | Fixer Output | Notes |
131
- | :-------------------------------: | ------------------------------- | ----------------------------------------------------------------------------------- |
132
- | `booleanVar === true` | `booleanVar` | |
133
- | `booleanVar !== true` | `!booleanVar` | |
134
- | `booleanVar === false` | `!booleanVar` | |
135
- | `booleanVar !== false` | `booleanVar` | |
136
- | `nullableBooleanVar === true` | `nullableBooleanVar` | Only checked/fixed if the `allowComparingNullableBooleansToTrue` option is `false` |
137
- | `nullableBooleanVar !== true` | `!nullableBooleanVar` | Only checked/fixed if the `allowComparingNullableBooleansToTrue` option is `false` |
138
- | `!(nullableBooleanVar === false)` | `nullableBooleanVar ?? true` | Only checked/fixed if the `allowComparingNullableBooleansToFalse` option is `false` |
139
- | `!(nullableBooleanVar !== false)` | `!(nullableBooleanVar ?? true)` | Only checked/fixed if the `allowComparingNullableBooleansToFalse` option is `false` |
130
+ | Comparison | Fixer Output | Notes |
131
+ | :----------------------------: | ------------------------------- | ----------------------------------------------------------------------------------- |
132
+ | `booleanVar === true` | `booleanVar` | |
133
+ | `booleanVar !== true` | `!booleanVar` | |
134
+ | `booleanVar === false` | `!booleanVar` | |
135
+ | `booleanVar !== false` | `booleanVar` | |
136
+ | `nullableBooleanVar === true` | `nullableBooleanVar` | Only checked/fixed if the `allowComparingNullableBooleansToTrue` option is `false` |
137
+ | `nullableBooleanVar !== true` | `!nullableBooleanVar` | Only checked/fixed if the `allowComparingNullableBooleansToTrue` option is `false` |
138
+ | `nullableBooleanVar === false` | `!(nullableBooleanVar ?? true)` | Only checked/fixed if the `allowComparingNullableBooleansToFalse` option is `false` |
139
+ | `nullableBooleanVar !== false` | `nullableBooleanVar ?? true` | Only checked/fixed if the `allowComparingNullableBooleansToFalse` option is `false` |
140
140
 
141
141
  ## When Not To Use It
142
142