@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,78 +0,0 @@
1
- ---
2
- description: 'Require using Error objects as Promise rejection reasons.'
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/prefer-promise-reject-errors** for documentation.
11
-
12
- It uses type information to enforce that `Promise`s are only rejected with `Error` objects.
13
-
14
- ## Examples
15
-
16
- <Tabs>
17
- <TabItem value="❌ Incorrect">
18
-
19
- ```ts
20
- Promise.reject('error');
21
-
22
- const err = new Error();
23
- Promise.reject('an ' + err);
24
-
25
- new Promise((resolve, reject) => reject('error'));
26
-
27
- new Promise((resolve, reject) => {
28
- const err = new Error();
29
- reject('an ' + err);
30
- });
31
- ```
32
-
33
- </TabItem>
34
- <TabItem value="✅ Correct">
35
-
36
- ```ts
37
- Promise.reject(new Error());
38
-
39
- class CustomError extends Error {
40
- // ...
41
- }
42
- Promise.reject(new CustomError());
43
-
44
- new Promise((resolve, reject) => reject(new Error()));
45
-
46
- new Promise((resolve, reject) => {
47
- class CustomError extends Error {
48
- // ...
49
- }
50
- return reject(new CustomError());
51
- });
52
- ```
53
-
54
- </TabItem>
55
- </Tabs>
56
-
57
- ## Options
58
-
59
- This rule adds the following options:
60
-
61
- ```ts
62
- interface Options {
63
- /**
64
- * Whether to always allow throwing values typed as `any`.
65
- */
66
- allowThrowingAny?: boolean;
67
-
68
- /**
69
- * Whether to always allow throwing values typed as `unknown`.
70
- */
71
- allowThrowingUnknown?: boolean;
72
- }
73
-
74
- const defaultOptions: Options = {
75
- allowThrowingAny: false,
76
- allowThrowingUnknown: false,
77
- };
78
- ```
@@ -1,408 +0,0 @@
1
- ---
2
- description: 'Require function parameters to be typed as `readonly` to prevent accidental mutation of inputs.'
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/prefer-readonly-parameter-types** for documentation.
11
-
12
- Mutating function arguments can lead to confusing, hard to debug behavior.
13
- Whilst it's easy to implicitly remember to not modify function arguments, explicitly typing arguments as readonly provides clear contract to consumers.
14
- This contract makes it easier for a consumer to reason about if a function has side-effects.
15
-
16
- This rule allows you to enforce that function parameters resolve to readonly types.
17
- A type is considered readonly if:
18
-
19
- - it is a primitive type (`string`, `number`, `boolean`, `symbol`, or an enum),
20
- - it is a function signature type,
21
- - it is a readonly array type whose element type is considered readonly.
22
- - it is a readonly tuple type whose elements are all considered readonly.
23
- - it is an object type whose properties are all marked as readonly, and whose values are all considered readonly.
24
-
25
- ## Examples
26
-
27
- <Tabs>
28
- <TabItem value="❌ Incorrect">
29
-
30
- ```ts
31
- function array1(arg: string[]) {} // array is not readonly
32
- function array2(arg: readonly string[][]) {} // array element is not readonly
33
- function array3(arg: [string, number]) {} // tuple is not readonly
34
- function array4(arg: readonly [string[], number]) {} // tuple element is not readonly
35
- // the above examples work the same if you use ReadonlyArray<T> instead
36
-
37
- function object1(arg: { prop: string }) {} // property is not readonly
38
- function object2(arg: { readonly prop: string; prop2: string }) {} // not all properties are readonly
39
- function object3(arg: { readonly prop: { prop2: string } }) {} // nested property is not readonly
40
- // the above examples work the same if you use Readonly<T> instead
41
-
42
- interface CustomArrayType extends ReadonlyArray<string> {
43
- prop: string; // note: this property is mutable
44
- }
45
- function custom1(arg: CustomArrayType) {}
46
-
47
- interface CustomFunction {
48
- (): void;
49
- prop: string; // note: this property is mutable
50
- }
51
- function custom2(arg: CustomFunction) {}
52
-
53
- function union(arg: string[] | ReadonlyArray<number[]>) {} // not all types are readonly
54
-
55
- // rule also checks function types
56
- interface Foo {
57
- (arg: string[]): void;
58
- }
59
- interface Foo {
60
- new (arg: string[]): void;
61
- }
62
- const x = { foo(arg: string[]): void {} };
63
- function foo(arg: string[]);
64
- type Foo = (arg: string[]) => void;
65
- interface Foo {
66
- foo(arg: string[]): void;
67
- }
68
- ```
69
-
70
- </TabItem>
71
- <TabItem value="✅ Correct">
72
-
73
- ```ts
74
- function array1(arg: readonly string[]) {}
75
- function array2(arg: readonly (readonly string[])[]) {}
76
- function array3(arg: readonly [string, number]) {}
77
- function array4(arg: readonly [readonly string[], number]) {}
78
- // the above examples work the same if you use ReadonlyArray<T> instead
79
-
80
- function object1(arg: { readonly prop: string }) {}
81
- function object2(arg: { readonly prop: string; readonly prop2: string }) {}
82
- function object3(arg: { readonly prop: { readonly prop2: string } }) {}
83
- // the above examples work the same if you use Readonly<T> instead
84
-
85
- interface CustomArrayType extends ReadonlyArray<string> {
86
- readonly prop: string;
87
- }
88
- function custom1(arg: Readonly<CustomArrayType>) {}
89
- // interfaces that extend the array types are not considered arrays, and thus must be made readonly.
90
-
91
- interface CustomFunction {
92
- (): void;
93
- readonly prop: string;
94
- }
95
- function custom2(arg: CustomFunction) {}
96
-
97
- function union(arg: readonly string[] | ReadonlyArray<number>) {}
98
-
99
- function primitive1(arg: string) {}
100
- function primitive2(arg: number) {}
101
- function primitive3(arg: boolean) {}
102
- function primitive4(arg: unknown) {}
103
- function primitive5(arg: null) {}
104
- function primitive6(arg: undefined) {}
105
- function primitive7(arg: any) {}
106
- function primitive8(arg: never) {}
107
- function primitive9(arg: string | number | undefined) {}
108
-
109
- function fnSig(arg: () => void) {}
110
-
111
- enum Foo {
112
- a,
113
- b,
114
- }
115
- function enumArg(arg: Foo) {}
116
-
117
- function symb1(arg: symbol) {}
118
- const customSymbol = Symbol('a');
119
- function symb2(arg: typeof customSymbol) {}
120
-
121
- // function types
122
- interface Foo {
123
- (arg: readonly string[]): void;
124
- }
125
- interface Foo {
126
- new (arg: readonly string[]): void;
127
- }
128
- const x = { foo(arg: readonly string[]): void {} };
129
- function foo(arg: readonly string[]);
130
- type Foo = (arg: readonly string[]) => void;
131
- interface Foo {
132
- foo(arg: readonly string[]): void;
133
- }
134
- ```
135
-
136
- </TabItem>
137
- </Tabs>
138
-
139
- ## Options
140
-
141
- ### `allow`
142
-
143
- {/* insert option description */}
144
-
145
- Some complex types cannot easily be made readonly, for example the `HTMLElement` type or the `JQueryStatic` type from `@types/jquery`. This option allows you to globally disable reporting of such types.
146
-
147
- This option takes the shared [`TypeOrValueSpecifier` format](/packages/type-utils/type-or-value-specifier).
148
-
149
- Examples of code for this rule with:
150
-
151
- ```json
152
- {
153
- "allow": [
154
- { "from": "file", "name": "Foo" },
155
- { "from": "lib", "name": "HTMLElement" },
156
- { "from": "package", "name": "Bar", "package": "bar-lib" }
157
- ]
158
- }
159
- ```
160
-
161
- <Tabs>
162
- <TabItem value="❌ Incorrect">
163
-
164
- ```ts option='{"allow":[{"from":"file","name":"Foo"},{"from":"lib","name":"HTMLElement"},{"from":"package","name":"Bar","package":"bar-lib"}]}'
165
- interface ThisIsMutable {
166
- prop: string;
167
- }
168
-
169
- interface Wrapper {
170
- sub: ThisIsMutable;
171
- }
172
-
173
- interface WrapperWithOther {
174
- readonly sub: Foo;
175
- otherProp: string;
176
- }
177
-
178
- // Incorrect because ThisIsMutable is not readonly
179
- function fn1(arg: ThisIsMutable) {}
180
-
181
- // Incorrect because Wrapper.sub is not readonly
182
- function fn2(arg: Wrapper) {}
183
-
184
- // Incorrect because WrapperWithOther.otherProp is not readonly and not in the allowlist
185
- function fn3(arg: WrapperWithOther) {}
186
- ```
187
-
188
- ```ts option='{"allow":[{"from":"file","name":"Foo"},{"from":"lib","name":"HTMLElement"},{"from":"package","name":"Bar","package":"bar-lib"}]}'
189
- import { Foo } from 'some-lib';
190
- import { Bar } from 'incorrect-lib';
191
-
192
- interface HTMLElement {
193
- prop: string;
194
- }
195
-
196
- // Incorrect because Foo is not a local type
197
- function fn1(arg: Foo) {}
198
-
199
- // Incorrect because HTMLElement is not from the default library
200
- function fn2(arg: HTMLElement) {}
201
-
202
- // Incorrect because Bar is not from "bar-lib"
203
- function fn3(arg: Bar) {}
204
- ```
205
-
206
- </TabItem>
207
- <TabItem value="✅ Correct">
208
-
209
- ```ts option='{"allow":[{"from":"file","name":"Foo"},{"from":"lib","name":"HTMLElement"},{"from":"package","name":"Bar","package":"bar-lib"}]}'
210
- interface Foo {
211
- prop: string;
212
- }
213
-
214
- interface Wrapper {
215
- readonly sub: Foo;
216
- readonly otherProp: string;
217
- }
218
-
219
- // Works because Foo is allowed
220
- function fn1(arg: Foo) {}
221
-
222
- // Works even when Foo is nested somewhere in the type, with other properties still being checked
223
- function fn2(arg: Wrapper) {}
224
- ```
225
-
226
- ```ts option='{"allow":[{"from":"file","name":"Foo"},{"from":"lib","name":"HTMLElement"},{"from":"package","name":"Bar","package":"bar-lib"}]}'
227
- import { Bar } from 'bar-lib';
228
-
229
- interface Foo {
230
- prop: string;
231
- }
232
-
233
- // Works because Foo is a local type
234
- function fn1(arg: Foo) {}
235
-
236
- // Works because HTMLElement is from the default library
237
- function fn2(arg: HTMLElement) {}
238
-
239
- // Works because Bar is from "bar-lib"
240
- function fn3(arg: Bar) {}
241
- ```
242
-
243
- ```ts option='{"allow":[{"from":"file","name":"Foo"},{"from":"lib","name":"HTMLElement"},{"from":"package","name":"Bar","package":"bar-lib"}]}'
244
- import { Foo } from './foo';
245
-
246
- // Works because Foo is still a local type - it has to be in the same package
247
- function fn(arg: Foo) {}
248
- ```
249
-
250
- </TabItem>
251
- </Tabs>
252
-
253
- ### `checkParameterProperties`
254
-
255
- {/* insert option description */}
256
-
257
- Because parameter properties create properties on the class, it may be undesirable to force them to be readonly.
258
-
259
- Examples of code for this rule with `{checkParameterProperties: true}`:
260
-
261
- <Tabs>
262
- <TabItem value="❌ Incorrect">
263
-
264
- ```ts option='{ "checkParameterProperties": true }'
265
- class Foo {
266
- constructor(private paramProp: string[]) {}
267
- }
268
- ```
269
-
270
- </TabItem>
271
- <TabItem value="✅ Correct">
272
-
273
- ```ts option='{ "checkParameterProperties": true }'
274
- class Foo {
275
- constructor(private paramProp: readonly string[]) {}
276
- }
277
- ```
278
-
279
- </TabItem>
280
- </Tabs>
281
-
282
- Examples of **correct** code for this rule with `{checkParameterProperties: false}`:
283
-
284
- ```ts option='{ "checkParameterProperties": false }' showPlaygroundButton
285
- class Foo {
286
- constructor(
287
- private paramProp1: string[],
288
- private paramProp2: readonly string[],
289
- ) {}
290
- }
291
- ```
292
-
293
- ### `ignoreInferredTypes`
294
-
295
- {/* insert option description */}
296
-
297
- This may be desirable in cases where an external dependency specifies a callback with mutable parameters, and manually annotating the callback's parameters is undesirable.
298
-
299
- Examples of code for this rule with `{ignoreInferredTypes: true}`:
300
-
301
- <Tabs>
302
- <TabItem value="❌ Incorrect">
303
-
304
- ```ts option='{ "ignoreInferredTypes": true }' skipValidation
305
- import { acceptsCallback, CallbackOptions } from 'external-dependency';
306
-
307
- acceptsCallback((options: CallbackOptions) => {});
308
- ```
309
-
310
- <details>
311
- <summary>external-dependency.d.ts</summary>
312
-
313
- ```ts option='{ "ignoreInferredTypes": true }'
314
- export interface CallbackOptions {
315
- prop: string;
316
- }
317
- type Callback = (options: CallbackOptions) => void;
318
- type AcceptsCallback = (callback: Callback) => void;
319
-
320
- export const acceptsCallback: AcceptsCallback;
321
- ```
322
-
323
- </details>
324
-
325
- </TabItem>
326
- <TabItem value="✅ Correct">
327
-
328
- ```ts option='{ "ignoreInferredTypes": true }'
329
- import { acceptsCallback } from 'external-dependency';
330
-
331
- acceptsCallback(options => {});
332
- ```
333
-
334
- <details>
335
- <summary>external-dependency.d.ts</summary>
336
-
337
- ```ts option='{ "ignoreInferredTypes": true }' skipValidation
338
- export interface CallbackOptions {
339
- prop: string;
340
- }
341
- type Callback = (options: CallbackOptions) => void;
342
- type AcceptsCallback = (callback: Callback) => void;
343
-
344
- export const acceptsCallback: AcceptsCallback;
345
- ```
346
-
347
- </details>
348
-
349
- </TabItem>
350
- </Tabs>
351
-
352
- ### `treatMethodsAsReadonly`
353
-
354
- {/* insert option description */}
355
-
356
- This may be desirable when you are never reassigning methods.
357
-
358
- Examples of code for this rule with `{treatMethodsAsReadonly: false}`:
359
-
360
- <Tabs>
361
- <TabItem value="❌ Incorrect">
362
-
363
- ```ts option='{ "treatMethodsAsReadonly": false }'
364
- type MyType = {
365
- readonly prop: string;
366
- method(): string; // note: this method is mutable
367
- };
368
- function foo(arg: MyType) {}
369
- ```
370
-
371
- </TabItem>
372
- <TabItem value="✅ Correct">
373
-
374
- ```ts option='{ "treatMethodsAsReadonly": false }'
375
- type MyType = Readonly<{
376
- prop: string;
377
- method(): string;
378
- }>;
379
- function foo(arg: MyType) {}
380
-
381
- type MyOtherType = {
382
- readonly prop: string;
383
- readonly method: () => string;
384
- };
385
- function bar(arg: MyOtherType) {}
386
- ```
387
-
388
- </TabItem>
389
- </Tabs>
390
-
391
- Examples of **correct** code for this rule with `{treatMethodsAsReadonly: true}`:
392
-
393
- ```ts option='{ "treatMethodsAsReadonly": true }' showPlaygroundButton
394
- type MyType = {
395
- readonly prop: string;
396
- method(): string; // note: this method is mutable
397
- };
398
- function foo(arg: MyType) {}
399
- ```
400
-
401
- ## When Not To Use It
402
-
403
- If your project does not attempt to enforce strong immutability guarantees of parameters, you can avoid this rule.
404
-
405
- This rule is very strict on what it considers mutable.
406
- Many types that describe themselves as readonly are considered mutable because they have mutable properties such as arrays or tuples.
407
- To work around these limitations, you might need to use the rule's options.
408
- In particular, the [`allow` option](#allow) can explicitly mark a type as readonly.
@@ -1,111 +0,0 @@
1
- ---
2
- description: "Require private members to be marked as `readonly` if they're never modified outside of the 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/prefer-readonly** for documentation.
11
-
12
- Private member variables (whether using the `private` modifier or private `#` fields) are never permitted to be modified outside of their declaring class.
13
- If that class never modifies their value, they may safely be marked as `readonly`.
14
-
15
- This rule reports on private members are marked as `readonly` if they're never modified outside of the constructor.
16
-
17
- ## Examples
18
-
19
- <Tabs>
20
- <TabItem value="❌ Incorrect">
21
-
22
- ```ts
23
- class Container {
24
- // These member variables could be marked as readonly
25
- private neverModifiedMember = true;
26
- private onlyModifiedInConstructor: number;
27
- #neverModifiedPrivateField = 3;
28
-
29
- public constructor(
30
- onlyModifiedInConstructor: number,
31
- // Private parameter properties can also be marked as readonly
32
- private neverModifiedParameter: string,
33
- ) {
34
- this.onlyModifiedInConstructor = onlyModifiedInConstructor;
35
- }
36
- }
37
- ```
38
-
39
- </TabItem>
40
- <TabItem value="✅ Correct">
41
-
42
- ```ts
43
- class Container {
44
- // Public members might be modified externally
45
- public publicMember: boolean;
46
-
47
- // Protected members might be modified by child classes
48
- protected protectedMember: number;
49
-
50
- // This is modified later on by the class
51
- private modifiedLater = 'unchanged';
52
-
53
- public mutate() {
54
- this.modifiedLater = 'mutated';
55
- }
56
-
57
- // This is modified later on by the class
58
- #modifiedLaterPrivateField = 'unchanged';
59
-
60
- public mutatePrivateField() {
61
- this.#modifiedLaterPrivateField = 'mutated';
62
- }
63
- }
64
- ```
65
-
66
- </TabItem>
67
- </Tabs>
68
-
69
- ## Options
70
-
71
- ### `onlyInlineLambdas`
72
-
73
- {/* insert option description */}
74
-
75
- ```jsonc
76
- {
77
- "@typescript-eslint/prefer-readonly": [
78
- "error",
79
- { "onlyInlineLambdas": true },
80
- ],
81
- }
82
- ```
83
-
84
- Example of code for the `{ "onlyInlineLambdas": true }` options:
85
-
86
- <Tabs>
87
- <TabItem value="❌ Incorrect">
88
-
89
- ```ts option='{ "onlyInlineLambdas": true }'
90
- class Container {
91
- private onClick = () => {
92
- /* ... */
93
- };
94
- }
95
- ```
96
-
97
- </TabItem>
98
- <TabItem value="✅ Correct">
99
-
100
- ```ts option='{ "onlyInlineLambdas": true }'
101
- class Container {
102
- private neverModifiedPrivate = 'unchanged';
103
- }
104
- ```
105
-
106
- </TabItem>
107
- </Tabs>
108
-
109
- ## When Not To Use It
110
-
111
- If you aren't trying to enforce strong immutability guarantees, this rule may be too restrictive for your project.
@@ -1,66 +0,0 @@
1
- ---
2
- description: 'Enforce using type parameter when calling `Array#reduce` instead of using a type assertion.'
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/prefer-reduce-type-parameter** for documentation.
11
-
12
- It's common to call `Array#reduce` with a generic type, such as an array or object, as the initial value.
13
- Since these values are empty, their types are not usable:
14
-
15
- - `[]` has type `never[]`, which can't have items pushed into it as nothing is type `never`
16
- - `{}` has type `{}`, which doesn't have an index signature and so can't have properties added to it
17
-
18
- A common solution to this problem is to use an `as` assertion on the initial value.
19
- While this will work, it's not the most optimal solution as type assertions have subtle effects on the underlying types that can allow bugs to slip in.
20
-
21
- A better solution is to pass the type in as a generic type argument to `Array#reduce` explicitly.
22
- This means that TypeScript doesn't have to try to infer the type, and avoids the common pitfalls that come with assertions.
23
-
24
- This rule looks for calls to `Array#reduce`, and reports if an initial value is being passed & asserted.
25
- It will suggest instead pass the asserted type to `Array#reduce` as a generic type argument.
26
-
27
- ## Examples
28
-
29
- <Tabs>
30
- <TabItem value="❌ Incorrect">
31
-
32
- ```ts
33
- [1, 2, 3].reduce((arr, num) => arr.concat(num * 2), [] as number[]);
34
-
35
- ['a', 'b'].reduce(
36
- (accum, name) => ({
37
- ...accum,
38
- [name]: true,
39
- }),
40
- {} as Record<string, boolean>,
41
- );
42
- ```
43
-
44
- </TabItem>
45
- <TabItem value="✅ Correct">
46
-
47
- ```ts
48
- [1, 2, 3].reduce<number[]>((arr, num) => arr.concat(num * 2), []);
49
-
50
- ['a', 'b'].reduce<Record<string, boolean>>(
51
- (accum, name) => ({
52
- ...accum,
53
- [name]: true,
54
- }),
55
- {},
56
- );
57
- ```
58
-
59
- </TabItem>
60
- </Tabs>
61
-
62
- ## When Not To Use It
63
-
64
- This rule can sometimes be difficult to work around when creating objects using a `.reduce`.
65
- See [[prefer-reduce-type-parameter] unfixable reporting #3440](https://github.com/typescript-eslint/typescript-eslint/issues/3440) for more details.
66
- 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.