@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,61 +0,0 @@
1
- ---
2
- description: 'Disallow the `void` operator except when used to discard a value.'
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-meaningless-void-operator** for documentation.
11
-
12
- `void` in TypeScript refers to a function return that is meant to be ignored.
13
- The `void` operator is a useful tool to convey the programmer's intent to discard a value.
14
- For example, it is recommended as one way of suppressing [`@typescript-eslint/no-floating-promises`](./no-floating-promises.mdx) instead of adding `.catch()` to a promise.
15
-
16
- This rule helps an authors catch API changes where previously a value was being discarded at a call site, but the callee changed so it no longer returns a value.
17
- When combined with [no-unused-expressions](https://eslint.org/docs/rules/no-unused-expressions), it also helps _readers_ of the code by ensuring consistency: a statement that looks like `void foo();` is **always** discarding a return value, and a statement that looks like `foo();` is **never** discarding a return value.
18
- This rule reports on any `void` operator whose argument is already of type `void` or `undefined`.
19
-
20
- ## Examples
21
-
22
- ## Examples
23
-
24
- <Tabs>
25
- <TabItem value="❌ Incorrect">
26
-
27
- ```ts
28
- void (() => {})();
29
-
30
- function foo() {}
31
- void foo();
32
- ```
33
-
34
- </TabItem>
35
- <TabItem value="✅ Correct">
36
-
37
- ```ts
38
- (() => {})();
39
-
40
- function foo() {}
41
- foo(); // nothing to discard
42
-
43
- function bar(x: number) {
44
- void x; // discarding a number
45
- return 2;
46
- }
47
- void bar(1); // discarding a number
48
- ```
49
-
50
- </TabItem>
51
- </Tabs>
52
-
53
- ## Options
54
-
55
- ### `checkNever`
56
-
57
- {/* insert option description */}
58
-
59
- ## When Not To Use It
60
-
61
- If you don't mind extra `void`s in your project, you can avoid this rule.
@@ -1,53 +0,0 @@
1
- ---
2
- description: 'Enforce valid definition of `new` and `constructor`.'
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-misused-new** for documentation.
11
-
12
- JavaScript classes may define a `constructor` method that runs when a class instance is newly created.
13
- TypeScript allows interfaces that describe a static class object to define a `new()` method (though this is rarely used in real world code).
14
- Developers new to JavaScript classes and/or TypeScript interfaces may sometimes confuse when to use `constructor` or `new`.
15
-
16
- This rule reports when a class defines a method named `new` or an interface defines a method named `constructor`.
17
-
18
- ## Examples
19
-
20
- <Tabs>
21
- <TabItem value="❌ Incorrect">
22
-
23
- ```ts
24
- declare class C {
25
- new(): C;
26
- }
27
-
28
- interface I {
29
- new (): I;
30
- constructor(): void;
31
- }
32
- ```
33
-
34
- </TabItem>
35
- <TabItem value="✅ Correct">
36
-
37
- ```ts
38
- declare class C {
39
- constructor();
40
- }
41
-
42
- interface I {
43
- new (): C;
44
- }
45
- ```
46
-
47
- </TabItem>
48
- </Tabs>
49
-
50
- ## When Not To Use It
51
-
52
- If you intentionally want a class with a `new` method, and you're confident nobody working in your code will mistake it with a constructor, you might not want this rule.
53
- 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.
@@ -1,314 +0,0 @@
1
- ---
2
- description: 'Disallow Promises in places not designed to handle them.'
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-misused-promises** for documentation.
11
-
12
- This rule forbids providing Promises to logical locations such as if statements in places where the TypeScript compiler allows them but they are not handled properly.
13
- These situations can often arise due to a missing `await` keyword or just a misunderstanding of the way async
14
- functions are handled/awaited.
15
-
16
- :::tip
17
- `no-misused-promises` only detects code that provides Promises to incorrect _logical_ locations.
18
- See [`no-floating-promises`](./no-floating-promises.mdx) for detecting unhandled Promise _statements_.
19
- :::
20
-
21
- ## Options
22
-
23
- ### `checksConditionals`
24
-
25
- {/* insert option description */}
26
-
27
- If you don't want to check conditionals, you can configure the rule with `"checksConditionals": false`:
28
-
29
- ```json
30
- {
31
- "@typescript-eslint/no-misused-promises": [
32
- "error",
33
- {
34
- "checksConditionals": false
35
- }
36
- ]
37
- }
38
- ```
39
-
40
- Doing so prevents the rule from looking at code like `if (somePromise)`.
41
-
42
- ### `checksVoidReturn`
43
-
44
- {/* insert option description */}
45
-
46
- Likewise, if you don't want to check functions that return promises where a void return is
47
- expected, your configuration will look like this:
48
-
49
- ```json
50
- {
51
- "@typescript-eslint/no-misused-promises": [
52
- "error",
53
- {
54
- "checksVoidReturn": false
55
- }
56
- ]
57
- }
58
- ```
59
-
60
- You can disable selective parts of the `checksVoidReturn` option by providing an object that disables specific checks. For example, if you don't mind that passing a `() => Promise<void>` to a `() => void` parameter or JSX attribute can lead to a floating unhandled Promise:
61
-
62
- ```json
63
- {
64
- "@typescript-eslint/no-misused-promises": [
65
- "error",
66
- {
67
- "checksVoidReturn": {
68
- "arguments": false,
69
- "attributes": false
70
- }
71
- }
72
- ]
73
- }
74
- ```
75
-
76
- The following sub-options are supported:
77
-
78
- #### `arguments`
79
-
80
- Disables checking an asynchronous function passed as argument where the parameter type expects a function that returns `void`.
81
-
82
- #### `attributes`
83
-
84
- Disables checking an asynchronous function passed as a JSX attribute expected to be a function that returns `void`.
85
-
86
- #### `inheritedMethods`
87
-
88
- Disables checking an asynchronous method in a type that extends or implements another type expecting that method to return `void`.
89
-
90
- :::note
91
- For now, `no-misused-promises` only checks _named_ methods against extended/implemented types: that is, call/construct/index signatures are ignored. Call signatures are not required in TypeScript to be consistent with one another, and construct signatures cannot be `async` in the first place. Index signature checking may be implemented in the future.
92
- :::
93
-
94
- #### `properties`
95
-
96
- Disables checking an asynchronous function passed as an object property expected to be a function that returns `void`.
97
-
98
- #### `returns`
99
-
100
- Disables checking an asynchronous function returned in a function whose return type is a function that returns `void`.
101
-
102
- #### `variables`
103
-
104
- Disables checking an asynchronous function used as a variable whose return type is a function that returns `void`.
105
-
106
- ### `checksSpreads`
107
-
108
- {/* insert option description */}
109
-
110
- If you don't want to check object spreads, you can add this configuration:
111
-
112
- ```json
113
- {
114
- "@typescript-eslint/no-misused-promises": [
115
- "error",
116
- {
117
- "checksSpreads": false
118
- }
119
- ]
120
- }
121
- ```
122
-
123
- ## Examples
124
-
125
- ### `checksConditionals`
126
-
127
- {/* insert option description */}
128
-
129
- Examples of code for this rule with `checksConditionals: true`:
130
-
131
- <Tabs>
132
- <TabItem value="❌ Incorrect">
133
-
134
- ```ts option='{ "checksConditionals": true }'
135
- const promise = Promise.resolve('value');
136
-
137
- if (promise) {
138
- // Do something
139
- }
140
-
141
- const val = promise ? 123 : 456;
142
-
143
- [1, 2, 3].filter(() => promise);
144
-
145
- while (promise) {
146
- // Do something
147
- }
148
- ```
149
-
150
- </TabItem>
151
- <TabItem value="✅ Correct">
152
-
153
- ```ts option='{ "checksConditionals": true }'
154
- const promise = Promise.resolve('value');
155
-
156
- // Always `await` the Promise in a conditional
157
- if (await promise) {
158
- // Do something
159
- }
160
-
161
- const val = (await promise) ? 123 : 456;
162
-
163
- const returnVal = await promise;
164
- [1, 2, 3].filter(() => returnVal);
165
-
166
- while (await promise) {
167
- // Do something
168
- }
169
- ```
170
-
171
- </TabItem>
172
- </Tabs>
173
-
174
- ### `checksVoidReturn`
175
-
176
- {/* insert option description */}
177
-
178
- Examples of code for this rule with `checksVoidReturn: true`:
179
-
180
- <Tabs>
181
- <TabItem value="❌ Incorrect">
182
-
183
- ```ts option='{ "checksVoidReturn": true }'
184
- [1, 2, 3].forEach(async value => {
185
- await fetch(`/${value}`);
186
- });
187
-
188
- new Promise<void>(async (resolve, reject) => {
189
- await fetch('/');
190
- resolve();
191
- });
192
-
193
- document.addEventListener('click', async () => {
194
- console.log('synchronous call');
195
- await fetch('/');
196
- console.log('synchronous call');
197
- });
198
-
199
- interface MySyncInterface {
200
- setThing(): void;
201
- }
202
- class MyClass implements MySyncInterface {
203
- async setThing(): Promise<void> {
204
- this.thing = await fetchThing();
205
- }
206
- }
207
- ```
208
-
209
- </TabItem>
210
- <TabItem value="✅ Correct">
211
-
212
- ```ts option='{ "checksVoidReturn": true }'
213
- // for-of puts `await` in outer context
214
- for (const value of [1, 2, 3]) {
215
- await doSomething(value);
216
- }
217
-
218
- // If outer context is not `async`, handle error explicitly
219
- Promise.all(
220
- [1, 2, 3].map(async value => {
221
- await doSomething(value);
222
- }),
223
- ).catch(handleError);
224
-
225
- // Use an async IIFE wrapper
226
- new Promise((resolve, reject) => {
227
- // combine with `void` keyword to tell `no-floating-promises` rule to ignore unhandled rejection
228
- void (async () => {
229
- await doSomething();
230
- resolve();
231
- })();
232
- });
233
-
234
- // Name the async wrapper to call it later
235
- document.addEventListener('click', () => {
236
- const handler = async () => {
237
- await doSomething();
238
- otherSynchronousCall();
239
- };
240
-
241
- try {
242
- synchronousCall();
243
- } catch (err) {
244
- handleSpecificError(err);
245
- }
246
-
247
- handler().catch(handleError);
248
- });
249
-
250
- interface MyAsyncInterface {
251
- setThing(): Promise<void>;
252
- }
253
- class MyClass implements MyAsyncInterface {
254
- async setThing(): Promise<void> {
255
- this.thing = await fetchThing();
256
- }
257
- }
258
- ```
259
-
260
- </TabItem>
261
- </Tabs>
262
-
263
- ### `checksSpreads`
264
-
265
- {/* insert option description */}
266
-
267
- Examples of code for this rule with `checksSpreads: true`:
268
-
269
- <Tabs>
270
- <TabItem value="❌ Incorrect">
271
-
272
- ```ts option='{ "checksSpreads": true }'
273
- const getData = () => fetch('/');
274
-
275
- console.log({ foo: 42, ...getData() });
276
-
277
- const awaitData = async () => {
278
- await fetch('/');
279
- };
280
-
281
- console.log({ foo: 42, ...awaitData() });
282
- ```
283
-
284
- </TabItem>
285
- <TabItem value="✅ Correct">
286
-
287
- ```ts option='{ "checksSpreads": true }'
288
- const getData = () => fetch('/');
289
-
290
- console.log({ foo: 42, ...(await getData()) });
291
-
292
- const awaitData = async () => {
293
- await fetch('/');
294
- };
295
-
296
- console.log({ foo: 42, ...(await awaitData()) });
297
- ```
298
-
299
- </TabItem>
300
- </Tabs>
301
-
302
- ## When Not To Use It
303
-
304
- This rule can be difficult to enable on large existing projects that set up many misused Promises.
305
- Alternately, if you're not worried about crashes from floating or misused Promises -such as if you have global unhandled Promise handlers registered- then in some cases it may be safe to not use this rule.
306
- 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.
307
-
308
- ## Further Reading
309
-
310
- - [TypeScript void function assignability](https://github.com/Microsoft/TypeScript/wiki/FAQ#why-are-functions-returning-non-void-assignable-to-function-returning-void)
311
-
312
- ## Related To
313
-
314
- - [`no-floating-promises`](./no-floating-promises.mdx)
@@ -1,132 +0,0 @@
1
- ---
2
- description: 'Disallow using the spread operator when it might cause unexpected behavior.'
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-misused-spread** for documentation.
11
-
12
- [Spread syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax) (`...`) is a JavaScript feature for creating an object with the joined properties of one or more other objects.
13
- TypeScript allows spreading objects whose properties are not typically meant to be enumerated, such as arrays and class instances.
14
-
15
- This rule disallows using the spread syntax on values whose types indicate doing so may cause unexpected behavior.
16
- That includes the following cases:
17
-
18
- - Spreading a `Promise` into an object.
19
- You probably meant to `await` it.
20
- - Spreading a function without properties into an object.
21
- You probably meant to call it.
22
- - Spreading an iterable (`Array`, `Map`, etc.) into an object.
23
- Iterable objects usually do not have meaningful enumerable properties and you probably meant to spread it into an array instead.
24
- - Spreading a string into an array.
25
- String enumeration behaviors in JavaScript around encoded characters are often surprising.
26
- - Spreading a `class` into an object.
27
- This copies all static own properties of the class, but none of the inheritance chain.
28
- - Spreading a class instance into an object.
29
- This does not faithfully copy the instance because only its own properties are copied, but the inheritance chain is lost, including all its methods.
30
-
31
- ## Examples
32
-
33
- <Tabs>
34
- <TabItem value="❌ Incorrect">
35
-
36
- ```ts
37
- declare const promise: Promise<number>;
38
- const spreadPromise = { ...promise };
39
-
40
- declare function getObject(): Record<string, strings>;
41
- const getObjectSpread = { ...getObject };
42
-
43
- declare const map: Map<string, number>;
44
- const mapSpread = { ...map };
45
-
46
- declare const userName: string;
47
- const characters = [...userName];
48
- ```
49
-
50
- ```ts
51
- declare class Box {
52
- value: number;
53
- }
54
- const boxSpread = { ...Box };
55
-
56
- declare const instance: Box;
57
- const instanceSpread = { ...instance };
58
- ```
59
-
60
- </TabItem>
61
- <TabItem value="✅ Correct">
62
-
63
- ```ts
64
- declare const promise: Promise<number>;
65
- const spreadPromise = { ...(await promise) };
66
-
67
- declare function getObject(): Record<string, strings>;
68
- const getObjectSpread = { ...getObject() };
69
-
70
- declare const map: Map<string, number>;
71
- const mapObject = Object.fromEntries(map);
72
-
73
- declare const userName: string;
74
- const characters = userName.split('');
75
- ```
76
-
77
- </TabItem>
78
- </Tabs>
79
-
80
- ## Options
81
-
82
- ### `allow`
83
-
84
- {/* insert option description */}
85
-
86
- This option takes the shared [`TypeOrValueSpecifier` format](/packages/type-utils/type-or-value-specifier).
87
-
88
- Examples of a configuration for this option in a `file.ts` file:
89
-
90
- ```json
91
- "@typescript-eslint/no-misused-spread": [
92
- "error",
93
- {
94
- "allow": [
95
- { "from": "file", "name": "BrandedString", "path": "file.ts" },
96
- ]
97
- }
98
- ]
99
- ```
100
-
101
- <Tabs>
102
- <TabItem value="❌ Incorrect">
103
-
104
- ```ts option='{"allow":[{ "from": "file", "name": "BrandedString" }]}'
105
- declare const unbrandedString: string;
106
-
107
- const spreadUnbrandedString = [...unbrandedString];
108
- ```
109
-
110
- </TabItem>
111
- <TabItem value="✅ Correct">
112
-
113
- ```ts option='{"allow":[{ "from": "file", "name": "BrandedString" }]}'
114
- type BrandedString = string & { __brand: 'safe' };
115
-
116
- declare const brandedString: BrandedString;
117
-
118
- const spreadBrandedString = [...brandedString];
119
- ```
120
-
121
- </TabItem>
122
- </Tabs>
123
-
124
- ## When Not To Use It
125
-
126
- If your application intentionally works with raw data in unusual ways, such as directly manipulating class prototype chains, you might not want this rule.
127
-
128
- If your use cases for unusual spreads only involve a few types, you might consider using [ESLint disable comments](https://eslint.org/docs/latest/use/configure/rules#using-configuration-comments-1) and/or the [`allow` option](#allow) instead of completely disabling this rule.
129
-
130
- ## Further Reading
131
-
132
- - [Strings Shouldn't Be Iterable By Default](https://www.xanthir.com/b4wJ1)
@@ -1,96 +0,0 @@
1
- ---
2
- description: 'Disallow enums from having both number and string members.'
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-mixed-enums** for documentation.
11
-
12
- TypeScript enums are allowed to assign numeric or string values to their members.
13
- Most enums contain either all numbers or all strings, but in theory you can mix-and-match within the same enum.
14
- Mixing enum member types is generally considered confusing and a bad practice.
15
-
16
- ## Examples
17
-
18
- <Tabs>
19
- <TabItem value="❌ Incorrect">
20
-
21
- ```ts
22
- enum Status {
23
- Unknown,
24
- Closed = 1,
25
- Open = 'open',
26
- }
27
- ```
28
-
29
- </TabItem>
30
- <TabItem value="✅ Correct (Explicit Numbers)">
31
-
32
- ```ts
33
- enum Status {
34
- Unknown = 0,
35
- Closed = 1,
36
- Open = 2,
37
- }
38
- ```
39
-
40
- </TabItem>
41
- <TabItem value="✅ Correct (Implicit Numbers)">
42
-
43
- ```ts
44
- enum Status {
45
- Unknown,
46
- Closed,
47
- Open,
48
- }
49
- ```
50
-
51
- </TabItem>
52
- <TabItem value="✅ Correct (Strings)">
53
-
54
- ```ts
55
- enum Status {
56
- Unknown = 'unknown',
57
- Closed = 'closed',
58
- Open = 'open',
59
- }
60
- ```
61
-
62
- </TabItem>
63
- </Tabs>
64
-
65
- ## Iteration Pitfalls of Mixed Enum Member Values
66
-
67
- Enum values may be iterated over using `Object.entries`/`Object.keys`/`Object.values`.
68
-
69
- If all enum members are strings, the number of items will match the number of enum members:
70
-
71
- ```ts
72
- enum Status {
73
- Closed = 'closed',
74
- Open = 'open',
75
- }
76
-
77
- // ['closed', 'open']
78
- Object.values(Status);
79
- ```
80
-
81
- But if the enum contains members that are initialized with numbers -including implicitly initialized numbers— then iteration over that enum will include those numbers as well:
82
-
83
- ```ts
84
- enum Status {
85
- Unknown,
86
- Closed = 1,
87
- Open = 'open',
88
- }
89
-
90
- // ["Unknown", "Closed", 0, 1, "open"]
91
- Object.values(Status);
92
- ```
93
-
94
- ## When Not To Use It
95
-
96
- If you don't mind the confusion of mixed enum member values and don't iterate over enums, you can safely disable this rule.