@typescript-eslint/eslint-plugin 8.0.0-alpha.5 → 8.0.0-alpha.51

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 (281) hide show
  1. package/dist/configs/all.js +7 -6
  2. package/dist/configs/all.js.map +1 -1
  3. package/dist/configs/disable-type-checked.js +3 -3
  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 +7 -2
  12. package/dist/configs/strict-type-checked-only.js.map +1 -1
  13. package/dist/configs/strict-type-checked.js +14 -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/array-type.js +14 -5
  26. package/dist/rules/array-type.js.map +1 -1
  27. package/dist/rules/class-literal-property-style.js +16 -16
  28. package/dist/rules/class-literal-property-style.js.map +1 -1
  29. package/dist/rules/class-methods-use-this.js.map +1 -1
  30. package/dist/rules/consistent-type-assertions.js +5 -19
  31. package/dist/rules/consistent-type-assertions.js.map +1 -1
  32. package/dist/rules/consistent-type-definitions.js +1 -1
  33. package/dist/rules/consistent-type-definitions.js.map +1 -1
  34. package/dist/rules/consistent-type-imports.js +10 -11
  35. package/dist/rules/consistent-type-imports.js.map +1 -1
  36. package/dist/rules/default-param-last.js +6 -1
  37. package/dist/rules/default-param-last.js.map +1 -1
  38. package/dist/rules/dot-notation.js +8 -1
  39. package/dist/rules/dot-notation.js.map +1 -1
  40. package/dist/rules/enum-utils/shared.js +3 -4
  41. package/dist/rules/enum-utils/shared.js.map +1 -1
  42. package/dist/rules/explicit-member-accessibility.js +49 -37
  43. package/dist/rules/explicit-member-accessibility.js.map +1 -1
  44. package/dist/rules/index.js +14 -6
  45. package/dist/rules/index.js.map +1 -1
  46. package/dist/rules/init-declarations.js +57 -1
  47. package/dist/rules/init-declarations.js.map +1 -1
  48. package/dist/rules/member-ordering.js +8 -2
  49. package/dist/rules/member-ordering.js.map +1 -1
  50. package/dist/rules/method-signature-style.js +1 -3
  51. package/dist/rules/method-signature-style.js.map +1 -1
  52. package/dist/rules/naming-convention-utils/parse-options.js +2 -5
  53. package/dist/rules/naming-convention-utils/parse-options.js.map +1 -1
  54. package/dist/rules/naming-convention-utils/shared.js +3 -4
  55. package/dist/rules/naming-convention-utils/shared.js.map +1 -1
  56. package/dist/rules/naming-convention-utils/validator.js +1 -2
  57. package/dist/rules/naming-convention-utils/validator.js.map +1 -1
  58. package/dist/rules/naming-convention.js +3 -1
  59. package/dist/rules/naming-convention.js.map +1 -1
  60. package/dist/rules/no-array-delete.js +1 -1
  61. package/dist/rules/no-array-delete.js.map +1 -1
  62. package/dist/rules/no-base-to-string.js +1 -1
  63. package/dist/rules/no-base-to-string.js.map +1 -1
  64. package/dist/rules/no-duplicate-type-constituents.js +1 -1
  65. package/dist/rules/no-duplicate-type-constituents.js.map +1 -1
  66. package/dist/rules/no-dynamic-delete.js +8 -40
  67. package/dist/rules/no-dynamic-delete.js.map +1 -1
  68. package/dist/rules/no-empty-interface.js +2 -1
  69. package/dist/rules/no-empty-interface.js.map +1 -1
  70. package/dist/rules/no-empty-object-type.js +141 -0
  71. package/dist/rules/no-empty-object-type.js.map +1 -0
  72. package/dist/rules/no-extraneous-class.js +11 -3
  73. package/dist/rules/no-extraneous-class.js.map +1 -1
  74. package/dist/rules/no-floating-promises.js +91 -55
  75. package/dist/rules/no-floating-promises.js.map +1 -1
  76. package/dist/rules/no-implied-eval.js +6 -24
  77. package/dist/rules/no-implied-eval.js.map +1 -1
  78. package/dist/rules/no-inferrable-types.js +1 -2
  79. package/dist/rules/no-inferrable-types.js.map +1 -1
  80. package/dist/rules/no-invalid-this.js +6 -0
  81. package/dist/rules/no-invalid-this.js.map +1 -1
  82. package/dist/rules/no-magic-numbers.js +32 -1
  83. package/dist/rules/no-magic-numbers.js.map +1 -1
  84. package/dist/rules/no-misused-new.js +4 -3
  85. package/dist/rules/no-misused-new.js.map +1 -1
  86. package/dist/rules/no-mixed-enums.js +6 -9
  87. package/dist/rules/no-mixed-enums.js.map +1 -1
  88. package/dist/rules/no-non-null-assertion.js +12 -14
  89. package/dist/rules/no-non-null-assertion.js.map +1 -1
  90. package/dist/rules/no-require-imports.js +13 -3
  91. package/dist/rules/no-require-imports.js.map +1 -1
  92. package/dist/rules/no-restricted-imports.js +5 -6
  93. package/dist/rules/no-restricted-imports.js.map +1 -1
  94. package/dist/rules/{ban-types.js → no-restricted-types.js} +17 -91
  95. package/dist/rules/no-restricted-types.js.map +1 -0
  96. package/dist/rules/no-shadow.js +4 -9
  97. package/dist/rules/no-shadow.js.map +1 -1
  98. package/dist/rules/no-unnecessary-condition.js +8 -6
  99. package/dist/rules/no-unnecessary-condition.js.map +1 -1
  100. package/dist/rules/no-unnecessary-parameter-property-assignment.js +151 -0
  101. package/dist/rules/no-unnecessary-parameter-property-assignment.js.map +1 -0
  102. package/dist/rules/no-unnecessary-qualifier.js.map +1 -1
  103. package/dist/rules/no-unnecessary-template-expression.js +223 -0
  104. package/dist/rules/no-unnecessary-template-expression.js.map +1 -0
  105. package/dist/rules/no-unnecessary-type-assertion.js +15 -13
  106. package/dist/rules/no-unnecessary-type-assertion.js.map +1 -1
  107. package/dist/rules/no-unnecessary-type-parameters.js +308 -0
  108. package/dist/rules/no-unnecessary-type-parameters.js.map +1 -0
  109. package/dist/rules/no-unsafe-argument.js +4 -2
  110. package/dist/rules/no-unsafe-argument.js.map +1 -1
  111. package/dist/rules/no-unsafe-assignment.js +24 -9
  112. package/dist/rules/no-unsafe-assignment.js.map +1 -1
  113. package/dist/rules/no-unsafe-call.js +5 -1
  114. package/dist/rules/no-unsafe-call.js.map +1 -1
  115. package/dist/rules/no-unsafe-enum-comparison.js +1 -5
  116. package/dist/rules/no-unsafe-enum-comparison.js.map +1 -1
  117. package/dist/rules/no-unsafe-function-type.js +47 -0
  118. package/dist/rules/no-unsafe-function-type.js.map +1 -0
  119. package/dist/rules/no-unsafe-member-access.js +10 -4
  120. package/dist/rules/no-unsafe-member-access.js.map +1 -1
  121. package/dist/rules/no-unsafe-return.js +9 -3
  122. package/dist/rules/no-unsafe-return.js.map +1 -1
  123. package/dist/rules/no-unsafe-unary-minus.js +1 -0
  124. package/dist/rules/no-unsafe-unary-minus.js.map +1 -1
  125. package/dist/rules/no-unused-expressions.js +1 -0
  126. package/dist/rules/no-unused-expressions.js.map +1 -1
  127. package/dist/rules/no-unused-vars.js +198 -79
  128. package/dist/rules/no-unused-vars.js.map +1 -1
  129. package/dist/rules/no-use-before-define.js +2 -16
  130. package/dist/rules/no-use-before-define.js.map +1 -1
  131. package/dist/rules/no-useless-constructor.js +1 -3
  132. package/dist/rules/no-useless-constructor.js.map +1 -1
  133. package/dist/rules/no-var-requires.js +2 -1
  134. package/dist/rules/no-var-requires.js.map +1 -1
  135. package/dist/rules/no-wrapper-object-types.js +61 -0
  136. package/dist/rules/no-wrapper-object-types.js.map +1 -0
  137. package/dist/rules/only-throw-error.js +1 -1
  138. package/dist/rules/only-throw-error.js.map +1 -1
  139. package/dist/rules/prefer-as-const.js +1 -1
  140. package/dist/rules/prefer-as-const.js.map +1 -1
  141. package/dist/rules/prefer-destructuring.js +3 -1
  142. package/dist/rules/prefer-destructuring.js.map +1 -1
  143. package/dist/rules/prefer-find.js +2 -1
  144. package/dist/rules/prefer-find.js.map +1 -1
  145. package/dist/rules/prefer-includes.js +1 -1
  146. package/dist/rules/prefer-includes.js.map +1 -1
  147. package/dist/rules/prefer-literal-enum-member.js +43 -2
  148. package/dist/rules/prefer-literal-enum-member.js.map +1 -1
  149. package/dist/rules/prefer-namespace-keyword.js +1 -1
  150. package/dist/rules/prefer-namespace-keyword.js.map +1 -1
  151. package/dist/rules/prefer-nullish-coalescing.js +8 -6
  152. package/dist/rules/prefer-nullish-coalescing.js.map +1 -1
  153. package/dist/rules/prefer-optional-chain-utils/analyzeChain.js +32 -37
  154. package/dist/rules/prefer-optional-chain-utils/analyzeChain.js.map +1 -1
  155. package/dist/rules/prefer-optional-chain-utils/checkNullishAndReport.js +1 -2
  156. package/dist/rules/prefer-optional-chain-utils/checkNullishAndReport.js.map +1 -1
  157. package/dist/rules/prefer-optional-chain-utils/compareNodes.js +1 -2
  158. package/dist/rules/prefer-optional-chain-utils/compareNodes.js.map +1 -1
  159. package/dist/rules/prefer-optional-chain-utils/gatherLogicalOperands.js +6 -11
  160. package/dist/rules/prefer-optional-chain-utils/gatherLogicalOperands.js.map +1 -1
  161. package/dist/rules/prefer-promise-reject-errors.js +1 -1
  162. package/dist/rules/prefer-promise-reject-errors.js.map +1 -1
  163. package/dist/rules/prefer-readonly.js +24 -12
  164. package/dist/rules/prefer-readonly.js.map +1 -1
  165. package/dist/rules/prefer-regexp-exec.js +15 -6
  166. package/dist/rules/prefer-regexp-exec.js.map +1 -1
  167. package/dist/rules/prefer-string-starts-ends-with.js.map +1 -1
  168. package/dist/rules/prefer-ts-expect-error.js +2 -1
  169. package/dist/rules/prefer-ts-expect-error.js.map +1 -1
  170. package/dist/rules/require-await.js +1 -1
  171. package/dist/rules/require-await.js.map +1 -1
  172. package/dist/rules/restrict-template-expressions.js +3 -3
  173. package/dist/rules/restrict-template-expressions.js.map +1 -1
  174. package/dist/rules/return-await.js +167 -86
  175. package/dist/rules/return-await.js.map +1 -1
  176. package/dist/rules/sort-type-constituents.js +23 -1
  177. package/dist/rules/sort-type-constituents.js.map +1 -1
  178. package/dist/rules/strict-boolean-expressions.js +102 -4
  179. package/dist/rules/strict-boolean-expressions.js.map +1 -1
  180. package/dist/rules/switch-exhaustiveness-check.js +7 -9
  181. package/dist/rules/switch-exhaustiveness-check.js.map +1 -1
  182. package/dist/rules/unified-signatures.js +3 -1
  183. package/dist/rules/unified-signatures.js.map +1 -1
  184. package/dist/util/astUtils.js +2 -3
  185. package/dist/util/astUtils.js.map +1 -1
  186. package/dist/util/collectUnusedVariables.js +110 -73
  187. package/dist/util/collectUnusedVariables.js.map +1 -1
  188. package/dist/util/createRule.js.map +1 -1
  189. package/dist/util/escapeRegExp.js +1 -2
  190. package/dist/util/escapeRegExp.js.map +1 -1
  191. package/dist/util/explicitReturnTypeUtils.js +11 -7
  192. package/dist/util/explicitReturnTypeUtils.js.map +1 -1
  193. package/dist/util/getESLintCoreRule.js +2 -2
  194. package/dist/util/getESLintCoreRule.js.map +1 -1
  195. package/dist/util/getForStatementHeadLoc.js +1 -2
  196. package/dist/util/getForStatementHeadLoc.js.map +1 -1
  197. package/dist/util/getFunctionHeadLoc.js +3 -4
  198. package/dist/util/getFunctionHeadLoc.js.map +1 -1
  199. package/dist/util/getMemberHeadLoc.js +80 -0
  200. package/dist/util/getMemberHeadLoc.js.map +1 -0
  201. package/dist/util/getOperatorPrecedence.js +5 -5
  202. package/dist/util/getOperatorPrecedence.js.map +1 -1
  203. package/dist/util/getStaticStringValue.js +1 -2
  204. package/dist/util/getStaticStringValue.js.map +1 -1
  205. package/dist/util/getStringLength.js +1 -2
  206. package/dist/util/getStringLength.js.map +1 -1
  207. package/dist/util/getTextWithParentheses.js +17 -0
  208. package/dist/util/getTextWithParentheses.js.map +1 -0
  209. package/dist/util/getThisExpression.js +1 -2
  210. package/dist/util/getThisExpression.js.map +1 -1
  211. package/dist/util/getWrappedCode.js +1 -2
  212. package/dist/util/getWrappedCode.js.map +1 -1
  213. package/dist/util/getWrappingFixer.js +2 -3
  214. package/dist/util/getWrappingFixer.js.map +1 -1
  215. package/dist/util/index.js +2 -0
  216. package/dist/util/index.js.map +1 -1
  217. package/dist/util/isAssignee.js +1 -2
  218. package/dist/util/isAssignee.js.map +1 -1
  219. package/dist/util/isNodeEqual.js +1 -2
  220. package/dist/util/isNodeEqual.js.map +1 -1
  221. package/dist/util/isNullLiteral.js +1 -2
  222. package/dist/util/isNullLiteral.js.map +1 -1
  223. package/dist/util/isTypeImport.js +22 -0
  224. package/dist/util/isTypeImport.js.map +1 -0
  225. package/dist/util/isUndefinedIdentifier.js +1 -2
  226. package/dist/util/isUndefinedIdentifier.js.map +1 -1
  227. package/dist/util/misc.js +14 -14
  228. package/dist/util/misc.js.map +1 -1
  229. package/dist/util/objectIterators.js +3 -4
  230. package/dist/util/objectIterators.js.map +1 -1
  231. package/dist/util/referenceContainsTypeQuery.js +20 -0
  232. package/dist/util/referenceContainsTypeQuery.js.map +1 -0
  233. package/dist/util/scopeUtils.js +11 -0
  234. package/dist/util/scopeUtils.js.map +1 -0
  235. package/docs/rules/array-type.mdx +4 -1
  236. package/docs/rules/ban-types.md +22 -0
  237. package/docs/rules/class-methods-use-this.mdx +1 -1
  238. package/docs/rules/consistent-indexed-object-style.mdx +1 -1
  239. package/docs/rules/consistent-type-imports.mdx +1 -1
  240. package/docs/rules/naming-convention.mdx +16 -0
  241. package/docs/rules/no-base-to-string.mdx +1 -1
  242. package/docs/rules/no-duplicate-imports.mdx +1 -1
  243. package/docs/rules/no-dynamic-delete.mdx +12 -7
  244. package/docs/rules/no-empty-interface.mdx +10 -0
  245. package/docs/rules/no-empty-object-type.mdx +145 -0
  246. package/docs/rules/no-extraneous-class.mdx +6 -0
  247. package/docs/rules/no-floating-promises.mdx +148 -4
  248. package/docs/rules/no-import-type-side-effects.mdx +1 -1
  249. package/docs/rules/no-misused-promises.mdx +18 -20
  250. package/docs/rules/no-require-imports.mdx +28 -1
  251. package/docs/rules/no-restricted-types.mdx +71 -0
  252. package/docs/rules/no-type-alias.mdx +1 -1
  253. package/docs/rules/no-unnecessary-boolean-literal-compare.mdx +12 -12
  254. package/docs/rules/no-unnecessary-parameter-property-assignment.mdx +42 -0
  255. package/docs/rules/no-unnecessary-template-expression.mdx +85 -0
  256. package/docs/rules/no-unnecessary-type-parameters.mdx +115 -0
  257. package/docs/rules/no-unsafe-function-type.mdx +63 -0
  258. package/docs/rules/no-unused-expressions.mdx +41 -1
  259. package/docs/rules/no-unused-vars.mdx +5 -0
  260. package/docs/rules/no-useless-template-literals.mdx +3 -59
  261. package/docs/rules/no-var-requires.mdx +6 -0
  262. package/docs/rules/no-wrapper-object-types.mdx +75 -0
  263. package/docs/rules/only-throw-error.mdx +7 -0
  264. package/docs/rules/prefer-readonly-parameter-types.mdx +10 -4
  265. package/docs/rules/prefer-ts-expect-error.mdx +10 -0
  266. package/docs/rules/require-await.mdx +2 -2
  267. package/docs/rules/restrict-template-expressions.mdx +1 -1
  268. package/docs/rules/return-await.mdx +119 -23
  269. package/docs/rules/sort-type-constituents.mdx +29 -9
  270. package/docs/rules/sort-type-union-intersection-members.mdx +12 -0
  271. package/docs/rules/strict-boolean-expressions.mdx +6 -0
  272. package/docs/rules/unbound-method.mdx +3 -2
  273. package/package.json +14 -19
  274. package/rules.d.ts +34 -3
  275. package/dist/rules/ban-types.js.map +0 -1
  276. package/dist/rules/no-throw-literal.js +0 -97
  277. package/dist/rules/no-throw-literal.js.map +0 -1
  278. package/dist/rules/no-useless-template-literals.js +0 -153
  279. package/dist/rules/no-useless-template-literals.js.map +0 -1
  280. package/docs/rules/ban-types.mdx +0 -138
  281. package/docs/rules/no-throw-literal.mdx +0 -25
@@ -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
 
@@ -0,0 +1,42 @@
1
+ ---
2
+ description: 'Disallow unnecessary assignment of constructor property parameter.'
3
+ ---
4
+
5
+ import Tabs from '@theme/Tabs';
6
+ import TabItem from '@theme/TabItem';
7
+
8
+ > 🛑 This file is source code, not the primary documentation location! 🛑
9
+ >
10
+ > See **https://typescript-eslint.io/rules/no-unnecessary-parameter-property-assignment** for documentation.
11
+
12
+ [TypeScript's parameter properties](https://www.typescriptlang.org/docs/handbook/2/classes.html#parameter-properties) allow creating and initializing a member in one place.
13
+ Therefore, in most cases, it is not necessary to assign parameter properties of the same name to members within a constructor.
14
+
15
+ ## Examples
16
+
17
+ <Tabs>
18
+ <TabItem value="❌ Incorrect">
19
+
20
+ ```ts
21
+ class Foo {
22
+ constructor(public bar: string) {
23
+ this.bar = bar;
24
+ }
25
+ }
26
+ ```
27
+
28
+ </TabItem>
29
+ <TabItem value="✅ Correct">
30
+
31
+ ```ts
32
+ class Foo {
33
+ constructor(public bar: string) {}
34
+ }
35
+ ```
36
+
37
+ </TabItem>
38
+ </Tabs>
39
+
40
+ ## When Not To Use It
41
+
42
+ If you don't use parameter properties, you can ignore this rule.
@@ -0,0 +1,85 @@
1
+ ---
2
+ description: 'Disallow unnecessary template expressions.'
3
+ ---
4
+
5
+ import Tabs from '@theme/Tabs';
6
+ import TabItem from '@theme/TabItem';
7
+
8
+ > 🛑 This file is source code, not the primary documentation location! 🛑
9
+ >
10
+ > See **https://typescript-eslint.io/rules/no-unnecessary-template-expression** for documentation.
11
+
12
+ This rule reports template literals that contain substitution expressions (also variously referred to as embedded expressions or string interpolations) that are unnecessary and can be simplified.
13
+
14
+ :::info[Migration from `no-useless-template-literals`]
15
+
16
+ This rule was formerly known as [`no-useless-template-literals`](./no-useless-template-literals.mdx).
17
+ The new name is a drop-in replacement with identical functionality.
18
+
19
+ :::
20
+
21
+ ## Examples
22
+
23
+ <Tabs>
24
+ <TabItem value="❌ Incorrect">
25
+
26
+ ```ts
27
+ // Static values can be incorporated into the surrounding template.
28
+
29
+ const ab1 = `${'a'}${'b'}`;
30
+ const ab2 = `a${'b'}`;
31
+
32
+ const stringWithNumber = `${'1 + 1 = '}${2}`;
33
+
34
+ const stringWithBoolean = `${'true is '}${true}`;
35
+
36
+ // Some simple expressions that are already strings
37
+ // can be rewritten without a template at all.
38
+
39
+ const text = 'a';
40
+ const wrappedText = `${text}`;
41
+
42
+ declare const intersectionWithString: string & { _brand: 'test-brand' };
43
+ const wrappedIntersection = `${intersectionWithString}`;
44
+ ```
45
+
46
+ </TabItem>
47
+ <TabItem value="✅ Correct">
48
+
49
+ ```ts
50
+ // Static values can be incorporated into the surrounding template.
51
+
52
+ const ab1 = `ab`;
53
+ const ab2 = `ab`;
54
+
55
+ const stringWithNumber = `1 + 1 = 2`;
56
+
57
+ const stringWithBoolean = `true is true`;
58
+
59
+ // Some simple expressions that are already strings
60
+ // can be rewritten without a template at all.
61
+
62
+ const text = 'a';
63
+ const wrappedText = text;
64
+
65
+ declare const intersectionWithString: string & { _brand: 'test-brand' };
66
+ const wrappedIntersection = intersectionWithString;
67
+ ```
68
+
69
+ </TabItem>
70
+ </Tabs>
71
+
72
+ :::info
73
+ This rule does not aim to flag template literals without substitution expressions that could have been written as an ordinary string.
74
+ That is to say, this rule will not help you turn `` `this` `` into `"this"`.
75
+ If you are looking for such a rule, you can configure the [`@stylistic/ts/quotes`](https://eslint.style/rules/ts/quotes) rule to do this.
76
+ :::
77
+
78
+ ## When Not To Use It
79
+
80
+ When you want to allow string expressions inside template literals.
81
+
82
+ ## Related To
83
+
84
+ - [`restrict-template-expressions`](./restrict-template-expressions.mdx)
85
+ - [`@stylistic/ts/quotes`](https://eslint.style/rules/ts/quotes)
@@ -0,0 +1,115 @@
1
+ ---
2
+ description: 'Disallow type parameters that only appear once.'
3
+ ---
4
+
5
+ import Tabs from '@theme/Tabs';
6
+ import TabItem from '@theme/TabItem';
7
+
8
+ > 🛑 This file is source code, not the primary documentation location! 🛑
9
+ >
10
+ > See **https://typescript-eslint.io/rules/no-unnecessary-type-parameters** for documentation.
11
+
12
+ This rule forbids type parameters that only appear once in a function, method, or class definition.
13
+
14
+ Type parameters relate two types.
15
+ If a type parameter only appears once, then it is not relating anything.
16
+ It can usually be replaced with explicit types such as `unknown`.
17
+
18
+ At best unnecessary type parameters make code harder to read.
19
+ At worst they can be used to disguise unsafe type assertions.
20
+
21
+ :::warning Early Stage
22
+ This rule was recently added to typescript-eslint and still considered experimental.
23
+ It might change significantly between minor versions.
24
+ Please try it out and give us feedback!
25
+ :::
26
+
27
+ ## Examples
28
+
29
+ <Tabs>
30
+ <TabItem value="❌ Incorrect">
31
+
32
+ ```ts
33
+ function second<A, B>(a: A, b: B): B {
34
+ return b;
35
+ }
36
+
37
+ function parseJSON<T>(input: string): T {
38
+ return JSON.parse(input);
39
+ }
40
+
41
+ function printProperty<T, K extends keyof T>(obj: T, key: K) {
42
+ console.log(obj[key]);
43
+ }
44
+ ```
45
+
46
+ </TabItem>
47
+ <TabItem value="✅ Correct">
48
+
49
+ ```ts
50
+ function second<B>(a: unknown, b: B): B {
51
+ return b;
52
+ }
53
+
54
+ function parseJSON(input: string): unknown {
55
+ return JSON.parse(input);
56
+ }
57
+
58
+ function printProperty<T>(obj: T, key: keyof T) {
59
+ console.log(obj[key]);
60
+ }
61
+
62
+ // T appears twice: in the type of arg and as the return type
63
+ function identity<T>(arg: T): T {
64
+ return arg;
65
+ }
66
+
67
+ // T appears twice: "keyof T" and in the inferred return type (T[K]).
68
+ // K appears twice: "key: K" and in the inferred return type (T[K]).
69
+ function getProperty<T, K extends keyof T>(obj: T, key: K) {
70
+ return obj[key];
71
+ }
72
+ ```
73
+
74
+ </TabItem>
75
+ </Tabs>
76
+
77
+ ## Limitations
78
+
79
+ Note that this rule allows any type parameter that is used multiple times, even if those uses are via a type argument.
80
+ For example, the following `T` is used multiple times by virtue of being in an `Array`, even though its name only appears once after declaration:
81
+
82
+ ```ts
83
+ declare function createStateHistory<T>(): T[];
84
+ ```
85
+
86
+ This is because the type parameter `T` relates multiple methods in the `T[]` together, making it used more than once.
87
+
88
+ Therefore, this rule won't report on type parameters used as a type argument.
89
+ That includes type arguments given to global types such as `Array` (including the `T[]` shorthand and in tuples), `Map`, and `Set`.
90
+
91
+ ## When Not To Use It
92
+
93
+ This rule will report on functions that use type parameters solely to test types, for example:
94
+
95
+ ```ts
96
+ function assertType<T>(arg: T) {}
97
+
98
+ assertType<number>(123);
99
+ assertType<number>('abc');
100
+ // ~~~~~
101
+ // Argument of type 'string' is not assignable to parameter of type 'number'.
102
+ ```
103
+
104
+ If you're using this pattern then you'll want to disable this rule on files that test types.
105
+
106
+ ## Further Reading
107
+
108
+ - TypeScript handbook: [Type Parameters Should Appear Twice](https://microsoft.github.io/TypeScript-New-Handbook/everything/#type-parameters-should-appear-twice)
109
+ - Effective TypeScript: [The Golden Rule of Generics](https://effectivetypescript.com/2020/08/12/generics-golden-rule/)
110
+
111
+ ## Related To
112
+
113
+ - eslint-plugin-etc's [`no-misused-generics`](https://github.com/cartant/eslint-plugin-etc/blob/main/docs/rules/no-misused-generics.md)
114
+ - wotan's [`no-misused-generics`](https://github.com/fimbullinter/wotan/blob/master/packages/mimir/docs/no-misused-generics.md)
115
+ - DefinitelyTyped-tools' [`no-unnecessary-generics`](https://github.com/microsoft/DefinitelyTyped-tools/blob/main/packages/eslint-plugin/docs/rules/no-unnecessary-generics.md)
@@ -0,0 +1,63 @@
1
+ ---
2
+ description: 'Disallow using the unsafe built-in Function 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-unsafe-function-type** for documentation.
11
+
12
+ TypeScript's built-in `Function` type allows being called with any number of arguments and returns type `any`.
13
+ `Function` also allows classes or plain objects that happen to possess all properties of the `Function` class.
14
+ It's generally better to specify function parameters and return types with the function type syntax.
15
+
16
+ "Catch-all" function types include:
17
+
18
+ - `() => void`: a function that has no parameters and whose return is ignored
19
+ - `(...args: never) => unknown`: a "top type" for functions that can be assigned any function type, but can't be called
20
+
21
+ Examples of code for this rule:
22
+
23
+ <Tabs>
24
+ <TabItem value="❌ Incorrect">
25
+
26
+ ```ts
27
+ let noParametersOrReturn: Function;
28
+ noParametersOrReturn = () => {};
29
+
30
+ let stringToNumber: Function;
31
+ stringToNumber = (text: string) => text.length;
32
+
33
+ let identity: Function;
34
+ identity = value => value;
35
+ ```
36
+
37
+ </TabItem>
38
+ <TabItem value="✅ Correct">
39
+
40
+ ```ts
41
+ let noParametersOrReturn: () => void;
42
+ noParametersOrReturn = () => {};
43
+
44
+ let stringToNumber: (text: string) => number;
45
+ stringToNumber = text => text.length;
46
+
47
+ let identity: <T>(value: T) => T;
48
+ identity = value => value;
49
+ ```
50
+
51
+ </TabItem>
52
+ </Tabs>
53
+
54
+ ## When Not To Use It
55
+
56
+ If your project is still onboarding to TypeScript, it might be difficult to fully replace all unsafe `Function` types with more precise function types.
57
+ 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.
58
+
59
+ ## Related To
60
+
61
+ - [`no-empty-object-type`](./no-empty-object-type.mdx)
62
+ - [`no-restricted-types`](./no-restricted-types.mdx)
63
+ - [`no-wrapper-object-types`](./no-wrapper-object-types.mdx)
@@ -10,4 +10,44 @@ import TabItem from '@theme/TabItem';
10
10
  > See **https://typescript-eslint.io/rules/no-unused-expressions** for documentation.
11
11
 
12
12
  This rule extends the base [`eslint/no-unused-expressions`](https://eslint.org/docs/rules/no-unused-expressions) rule.
13
- It adds support for optional call expressions `x?.()`, and directive in module declarations.
13
+ It supports TypeScript-specific expressions:
14
+
15
+ - Marks directives in modules declarations (`"use strict"`, etc.) as not unused
16
+ - Marks the following expressions as unused if their wrapped value expressions are unused:
17
+ - Assertion expressions: `x as number;`, `x!;`, `<number>x;`
18
+ - Instantiation expressions: `Set<number>;`
19
+
20
+ Although the type expressions never have runtime side effects (that is, `x!;` is the same as `x;`), they can be used to assert types for testing purposes.
21
+
22
+ ## Examples
23
+
24
+ <Tabs>
25
+ <TabItem value="❌ Incorrect">
26
+
27
+ ```ts
28
+ Set<number>;
29
+ 1 as number;
30
+ window!;
31
+ ```
32
+
33
+ </TabItem>
34
+ <TabItem value="✅ Correct">
35
+
36
+ ```ts
37
+ function getSet() {
38
+ return Set;
39
+ }
40
+
41
+ // Funtion calls are allowed, so type expressions that wrap function calls are allowed
42
+ getSet()<number>;
43
+ getSet() as Set<unknown>;
44
+ getSet()!;
45
+
46
+ // Namespaces can have directives
47
+ namespace A {
48
+ 'use strict';
49
+ }
50
+ ```
51
+
52
+ </TabItem>
53
+ </Tabs>
@@ -47,3 +47,8 @@ We generally recommend using `@typescript-eslint/no-unused-vars` to flag unused
47
47
  :::tip
48
48
  Editors such as VS Code will still generally "grey out" unused variables even if `noUnusedLocals` and `noUnusedParameters` are not enabled in a project.
49
49
  :::
50
+
51
+ Also see similar rules provided by ESLint:
52
+
53
+ - [`no-unused-private-class-members`](https://eslint.org/docs/latest/rules/no-unused-private-class-members)
54
+ - [`no-unused-labels`](https://eslint.org/docs/latest/rules/no-unused-labels)
@@ -1,61 +1,5 @@
1
- ---
2
- description: 'Disallow unnecessary template literals.'
3
- ---
1
+ :::danger Deprecated
4
2
 
5
- import Tabs from '@theme/Tabs';
6
- import TabItem from '@theme/TabItem';
3
+ This rule has been renamed to [`no-unnecessary-template-expression`](./no-unnecessary-template-expression.mdx). See [#8544](https://github.com/typescript-eslint/typescript-eslint/issues/8544) for more information.
7
4
 
8
- > 🛑 This file is source code, not the primary documentation location! 🛑
9
- >
10
- > See **https://typescript-eslint.io/rules/no-useless-template-literals** for documentation.
11
-
12
- This rule reports template literals that can be simplified to a normal string literal.
13
-
14
- ## Examples
15
-
16
- <Tabs>
17
- <TabItem value="❌ Incorrect">
18
-
19
- ```ts
20
- const ab1 = `${'a'}${'b'}`;
21
- const ab2 = `a${'b'}`;
22
-
23
- const stringWithNumber = `${'1 + 1 = '}${2}`;
24
-
25
- const stringWithBoolean = `${'true is '}${true}`;
26
-
27
- const text = 'a';
28
- const wrappedText = `${text}`;
29
-
30
- declare const intersectionWithString: string & { _brand: 'test-brand' };
31
- const wrappedIntersection = `${intersectionWithString}`;
32
- ```
33
-
34
- </TabItem>
35
- <TabItem value="✅ Correct">
36
-
37
- ```ts
38
- const ab1 = 'ab';
39
- const ab2 = 'ab';
40
-
41
- const stringWithNumber = `1 + 1 = 2`;
42
-
43
- const stringWithBoolean = `true is true`;
44
-
45
- const text = 'a';
46
- const wrappedText = text;
47
-
48
- declare const intersectionWithString: string & { _brand: 'test-brand' };
49
- const wrappedIntersection = intersectionWithString;
50
- ```
51
-
52
- </TabItem>
53
- </Tabs>
54
-
55
- ## When Not To Use It
56
-
57
- When you want to allow string expressions inside template literals.
58
-
59
- ## Related To
60
-
61
- - [`restrict-template-expressions`](./restrict-template-expressions.mdx)
5
+ :::