@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.
- package/package.json +9 -10
- package/docs/rules/README.md +0 -57
- package/docs/rules/TEMPLATE.md +0 -36
- package/docs/rules/adjacent-overload-signatures.mdx +0 -105
- package/docs/rules/array-type.mdx +0 -126
- package/docs/rules/await-thenable.mdx +0 -184
- package/docs/rules/ban-ts-comment.mdx +0 -165
- package/docs/rules/ban-tslint-comment.mdx +0 -45
- package/docs/rules/ban-types.md +0 -26
- package/docs/rules/block-spacing.md +0 -15
- package/docs/rules/brace-style.md +0 -15
- package/docs/rules/camelcase.md +0 -15
- package/docs/rules/class-literal-property-style.mdx +0 -112
- package/docs/rules/class-methods-use-this.mdx +0 -135
- package/docs/rules/comma-dangle.md +0 -15
- package/docs/rules/comma-spacing.md +0 -15
- package/docs/rules/consistent-generic-constructors.mdx +0 -87
- package/docs/rules/consistent-indexed-object-style.mdx +0 -105
- package/docs/rules/consistent-return.mdx +0 -51
- package/docs/rules/consistent-type-assertions.mdx +0 -196
- package/docs/rules/consistent-type-definitions.mdx +0 -133
- package/docs/rules/consistent-type-exports.mdx +0 -97
- package/docs/rules/consistent-type-imports.mdx +0 -139
- package/docs/rules/default-param-last.mdx +0 -59
- package/docs/rules/dot-notation.mdx +0 -94
- package/docs/rules/explicit-function-return-type.mdx +0 -359
- package/docs/rules/explicit-member-accessibility.mdx +0 -353
- package/docs/rules/explicit-module-boundary-types.mdx +0 -287
- package/docs/rules/func-call-spacing.md +0 -15
- package/docs/rules/indent.md +0 -15
- package/docs/rules/init-declarations.mdx +0 -12
- package/docs/rules/key-spacing.md +0 -15
- package/docs/rules/keyword-spacing.md +0 -15
- package/docs/rules/lines-around-comment.md +0 -15
- package/docs/rules/lines-between-class-members.md +0 -15
- package/docs/rules/max-params.mdx +0 -54
- package/docs/rules/member-delimiter-style.md +0 -15
- package/docs/rules/member-ordering.mdx +0 -1483
- package/docs/rules/method-signature-style.mdx +0 -124
- package/docs/rules/naming-convention.mdx +0 -755
- package/docs/rules/no-array-constructor.mdx +0 -34
- package/docs/rules/no-array-delete.mdx +0 -44
- package/docs/rules/no-base-to-string.mdx +0 -115
- package/docs/rules/no-confusing-non-null-assertion.mdx +0 -75
- package/docs/rules/no-confusing-void-expression.mdx +0 -148
- package/docs/rules/no-deprecated.mdx +0 -119
- package/docs/rules/no-dupe-class-members.mdx +0 -16
- package/docs/rules/no-duplicate-enum-values.mdx +0 -66
- package/docs/rules/no-duplicate-imports.mdx +0 -17
- package/docs/rules/no-duplicate-type-constituents.mdx +0 -89
- package/docs/rules/no-dynamic-delete.mdx +0 -64
- package/docs/rules/no-empty-function.mdx +0 -94
- package/docs/rules/no-empty-interface.mdx +0 -75
- package/docs/rules/no-empty-object-type.mdx +0 -150
- package/docs/rules/no-explicit-any.mdx +0 -177
- package/docs/rules/no-extra-non-null-assertion.mdx +0 -60
- package/docs/rules/no-extra-parens.md +0 -15
- package/docs/rules/no-extra-semi.md +0 -15
- package/docs/rules/no-extraneous-class.mdx +0 -329
- package/docs/rules/no-floating-promises.mdx +0 -282
- package/docs/rules/no-for-in-array.mdx +0 -67
- package/docs/rules/no-implied-eval.mdx +0 -106
- package/docs/rules/no-import-type-side-effects.mdx +0 -80
- package/docs/rules/no-inferrable-types.mdx +0 -113
- package/docs/rules/no-invalid-this.mdx +0 -16
- package/docs/rules/no-invalid-void-type.mdx +0 -119
- package/docs/rules/no-loop-func.mdx +0 -12
- package/docs/rules/no-loss-of-precision.mdx +0 -17
- package/docs/rules/no-magic-numbers.mdx +0 -131
- package/docs/rules/no-meaningless-void-operator.mdx +0 -61
- package/docs/rules/no-misused-new.mdx +0 -53
- package/docs/rules/no-misused-promises.mdx +0 -314
- package/docs/rules/no-misused-spread.mdx +0 -132
- package/docs/rules/no-mixed-enums.mdx +0 -96
- package/docs/rules/no-namespace.mdx +0 -157
- package/docs/rules/no-non-null-asserted-nullish-coalescing.mdx +0 -60
- package/docs/rules/no-non-null-asserted-optional-chain.mdx +0 -46
- package/docs/rules/no-non-null-assertion.mdx +0 -48
- package/docs/rules/no-parameter-properties.mdx +0 -16
- package/docs/rules/no-redeclare.mdx +0 -79
- package/docs/rules/no-redundant-type-constituents.mdx +0 -102
- package/docs/rules/no-require-imports.mdx +0 -114
- package/docs/rules/no-restricted-imports.mdx +0 -84
- package/docs/rules/no-restricted-types.mdx +0 -70
- package/docs/rules/no-shadow.mdx +0 -143
- package/docs/rules/no-this-alias.mdx +0 -124
- package/docs/rules/no-type-alias.mdx +0 -626
- package/docs/rules/no-unnecessary-boolean-literal-compare.mdx +0 -165
- package/docs/rules/no-unnecessary-condition.mdx +0 -293
- package/docs/rules/no-unnecessary-parameter-property-assignment.mdx +0 -42
- package/docs/rules/no-unnecessary-qualifier.mdx +0 -57
- package/docs/rules/no-unnecessary-template-expression.mdx +0 -108
- package/docs/rules/no-unnecessary-type-arguments.mdx +0 -85
- package/docs/rules/no-unnecessary-type-assertion.mdx +0 -97
- package/docs/rules/no-unnecessary-type-constraint.mdx +0 -61
- package/docs/rules/no-unnecessary-type-conversion.mdx +0 -79
- package/docs/rules/no-unnecessary-type-parameters.mdx +0 -255
- package/docs/rules/no-unsafe-argument.mdx +0 -98
- package/docs/rules/no-unsafe-assignment.mdx +0 -101
- package/docs/rules/no-unsafe-call.mdx +0 -120
- package/docs/rules/no-unsafe-declaration-merging.mdx +0 -65
- package/docs/rules/no-unsafe-enum-comparison.mdx +0 -98
- package/docs/rules/no-unsafe-function-type.mdx +0 -65
- package/docs/rules/no-unsafe-member-access.mdx +0 -81
- package/docs/rules/no-unsafe-return.mdx +0 -126
- package/docs/rules/no-unsafe-type-assertion.mdx +0 -63
- package/docs/rules/no-unsafe-unary-minus.mdx +0 -60
- package/docs/rules/no-unused-expressions.mdx +0 -52
- package/docs/rules/no-unused-vars.mdx +0 -136
- package/docs/rules/no-use-before-define.mdx +0 -98
- package/docs/rules/no-useless-constructor.mdx +0 -21
- package/docs/rules/no-useless-empty-export.mdx +0 -53
- package/docs/rules/no-useless-template-literals.mdx +0 -9
- package/docs/rules/no-var-requires.mdx +0 -77
- package/docs/rules/no-wrapper-object-types.mdx +0 -75
- package/docs/rules/non-nullable-type-assertion-style.mdx +0 -47
- package/docs/rules/object-curly-spacing.md +0 -15
- package/docs/rules/only-throw-error.mdx +0 -150
- package/docs/rules/padding-line-between-statements.md +0 -15
- package/docs/rules/parameter-properties.mdx +0 -522
- package/docs/rules/prefer-as-const.mdx +0 -51
- package/docs/rules/prefer-destructuring.mdx +0 -101
- package/docs/rules/prefer-enum-initializers.mdx +0 -68
- package/docs/rules/prefer-find.mdx +0 -45
- package/docs/rules/prefer-for-of.mdx +0 -50
- package/docs/rules/prefer-function-type.mdx +0 -98
- package/docs/rules/prefer-includes.mdx +0 -81
- package/docs/rules/prefer-literal-enum-member.mdx +0 -111
- package/docs/rules/prefer-namespace-keyword.mdx +0 -51
- package/docs/rules/prefer-nullish-coalescing.mdx +0 -349
- package/docs/rules/prefer-optional-chain.mdx +0 -304
- package/docs/rules/prefer-promise-reject-errors.mdx +0 -78
- package/docs/rules/prefer-readonly-parameter-types.mdx +0 -408
- package/docs/rules/prefer-readonly.mdx +0 -111
- package/docs/rules/prefer-reduce-type-parameter.mdx +0 -66
- package/docs/rules/prefer-regexp-exec.mdx +0 -52
- package/docs/rules/prefer-return-this-type.mdx +0 -93
- package/docs/rules/prefer-string-starts-ends-with.mdx +0 -84
- package/docs/rules/prefer-ts-expect-error.mdx +0 -86
- package/docs/rules/promise-function-async.mdx +0 -143
- package/docs/rules/quotes.md +0 -15
- package/docs/rules/related-getter-setter-pairs.mdx +0 -61
- package/docs/rules/require-array-sort-compare.mdx +0 -89
- package/docs/rules/require-await.mdx +0 -53
- package/docs/rules/restrict-plus-operands.mdx +0 -245
- package/docs/rules/restrict-template-expressions.mdx +0 -167
- package/docs/rules/return-await.mdx +0 -339
- package/docs/rules/semi.md +0 -15
- package/docs/rules/sort-type-constituents.mdx +0 -209
- package/docs/rules/sort-type-union-intersection-members.mdx +0 -16
- package/docs/rules/space-before-blocks.md +0 -15
- package/docs/rules/space-before-function-paren.md +0 -15
- package/docs/rules/space-infix-ops.md +0 -15
- package/docs/rules/strict-boolean-expressions.mdx +0 -184
- package/docs/rules/switch-exhaustiveness-check.mdx +0 -280
- package/docs/rules/triple-slash-reference.mdx +0 -129
- package/docs/rules/type-annotation-spacing.md +0 -15
- package/docs/rules/typedef.mdx +0 -350
- package/docs/rules/unbound-method.mdx +0 -114
- package/docs/rules/unified-signatures.mdx +0 -132
- package/docs/rules/use-unknown-in-catch-callback-variable.mdx +0 -97
@@ -1,522 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Require or disallow parameter properties in class constructors.'
|
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/parameter-properties** for documentation.
|
11
|
-
|
12
|
-
TypeScript includes a "parameter properties" shorthand for declaring a class constructor parameter and class property in one location.
|
13
|
-
Parameter properties can be confusing to those new to TypeScript as they are less explicit than other ways of declaring and initializing class members.
|
14
|
-
|
15
|
-
This rule can be configured to always disallow the use of parameter properties or enforce their usage when possible.
|
16
|
-
|
17
|
-
## Options
|
18
|
-
|
19
|
-
This rule, in its default state, does not require any argument and would completely disallow the use of parameter properties.
|
20
|
-
It may take an options object containing either or both of:
|
21
|
-
|
22
|
-
- `"allow"`: allowing certain kinds of properties to be ignored
|
23
|
-
- `"prefer"`: either `"class-property"` _(default)_ or `"parameter-property"`
|
24
|
-
|
25
|
-
### `allow`
|
26
|
-
|
27
|
-
{/* insert option description */}
|
28
|
-
|
29
|
-
If you would like to ignore certain kinds of properties then you may pass an object containing `"allow"` as an array of any of the following options:
|
30
|
-
|
31
|
-
- `allow`, an array containing one or more of the allowed modifiers. Valid values are:
|
32
|
-
- `readonly`, allows **readonly** parameter properties.
|
33
|
-
- `private`, allows **private** parameter properties.
|
34
|
-
- `protected`, allows **protected** parameter properties.
|
35
|
-
- `public`, allows **public** parameter properties.
|
36
|
-
- `private readonly`, allows **private readonly** parameter properties.
|
37
|
-
- `protected readonly`, allows **protected readonly** parameter properties.
|
38
|
-
- `public readonly`, allows **public readonly** parameter properties.
|
39
|
-
|
40
|
-
For example, to ignore `public` properties:
|
41
|
-
|
42
|
-
```json
|
43
|
-
{
|
44
|
-
"@typescript-eslint/parameter-properties": [
|
45
|
-
true,
|
46
|
-
{
|
47
|
-
"allow": ["public"]
|
48
|
-
}
|
49
|
-
]
|
50
|
-
}
|
51
|
-
```
|
52
|
-
|
53
|
-
### `prefer`
|
54
|
-
|
55
|
-
{/* insert option description */}
|
56
|
-
|
57
|
-
By default, the rule prefers class properties.
|
58
|
-
You can switch it to instead preferring parameter properties with (`"parameter-property"`).
|
59
|
-
|
60
|
-
In `"parameter-property"` mode, the rule will issue a report when:
|
61
|
-
|
62
|
-
- A class property and constructor parameter have the same name and type
|
63
|
-
- The constructor parameter is assigned to the class property at the beginning of the constructor
|
64
|
-
|
65
|
-
### default
|
66
|
-
|
67
|
-
Examples of code for this rule with no options at all:
|
68
|
-
|
69
|
-
<Tabs>
|
70
|
-
<TabItem value="❌ Incorrect">
|
71
|
-
|
72
|
-
```ts
|
73
|
-
class Foo {
|
74
|
-
constructor(readonly name: string) {}
|
75
|
-
}
|
76
|
-
|
77
|
-
class Foo {
|
78
|
-
constructor(private name: string) {}
|
79
|
-
}
|
80
|
-
|
81
|
-
class Foo {
|
82
|
-
constructor(protected name: string) {}
|
83
|
-
}
|
84
|
-
|
85
|
-
class Foo {
|
86
|
-
constructor(public name: string) {}
|
87
|
-
}
|
88
|
-
|
89
|
-
class Foo {
|
90
|
-
constructor(private readonly name: string) {}
|
91
|
-
}
|
92
|
-
|
93
|
-
class Foo {
|
94
|
-
constructor(protected readonly name: string) {}
|
95
|
-
}
|
96
|
-
|
97
|
-
class Foo {
|
98
|
-
constructor(public readonly name: string) {}
|
99
|
-
}
|
100
|
-
```
|
101
|
-
|
102
|
-
</TabItem>
|
103
|
-
<TabItem value="✅ Correct">
|
104
|
-
|
105
|
-
```ts
|
106
|
-
class Foo {
|
107
|
-
constructor(name: string) {}
|
108
|
-
}
|
109
|
-
```
|
110
|
-
|
111
|
-
</TabItem>
|
112
|
-
</Tabs>
|
113
|
-
|
114
|
-
### readonly
|
115
|
-
|
116
|
-
Examples of code for the `{ "allow": ["readonly"] }` options:
|
117
|
-
|
118
|
-
<Tabs>
|
119
|
-
<TabItem value="❌ Incorrect">
|
120
|
-
|
121
|
-
```ts option='{ "allow": ["readonly"] }'
|
122
|
-
class Foo {
|
123
|
-
constructor(private name: string) {}
|
124
|
-
}
|
125
|
-
|
126
|
-
class Foo {
|
127
|
-
constructor(protected name: string) {}
|
128
|
-
}
|
129
|
-
|
130
|
-
class Foo {
|
131
|
-
constructor(public name: string) {}
|
132
|
-
}
|
133
|
-
|
134
|
-
class Foo {
|
135
|
-
constructor(private readonly name: string) {}
|
136
|
-
}
|
137
|
-
|
138
|
-
class Foo {
|
139
|
-
constructor(protected readonly name: string) {}
|
140
|
-
}
|
141
|
-
|
142
|
-
class Foo {
|
143
|
-
constructor(public readonly name: string) {}
|
144
|
-
}
|
145
|
-
```
|
146
|
-
|
147
|
-
</TabItem>
|
148
|
-
<TabItem value="✅ Correct">
|
149
|
-
|
150
|
-
```ts option='{ "allow": ["readonly"] }'
|
151
|
-
class Foo {
|
152
|
-
constructor(name: string) {}
|
153
|
-
}
|
154
|
-
|
155
|
-
class Foo {
|
156
|
-
constructor(readonly name: string) {}
|
157
|
-
}
|
158
|
-
```
|
159
|
-
|
160
|
-
</TabItem>
|
161
|
-
</Tabs>
|
162
|
-
|
163
|
-
### private
|
164
|
-
|
165
|
-
Examples of code for the `{ "allow": ["private"] }` options:
|
166
|
-
|
167
|
-
<Tabs>
|
168
|
-
<TabItem value="❌ Incorrect">
|
169
|
-
|
170
|
-
```ts option='{ "allow": ["private"] }'
|
171
|
-
class Foo {
|
172
|
-
constructor(readonly name: string) {}
|
173
|
-
}
|
174
|
-
|
175
|
-
class Foo {
|
176
|
-
constructor(protected name: string) {}
|
177
|
-
}
|
178
|
-
|
179
|
-
class Foo {
|
180
|
-
constructor(public name: string) {}
|
181
|
-
}
|
182
|
-
|
183
|
-
class Foo {
|
184
|
-
constructor(private readonly name: string) {}
|
185
|
-
}
|
186
|
-
|
187
|
-
class Foo {
|
188
|
-
constructor(protected readonly name: string) {}
|
189
|
-
}
|
190
|
-
|
191
|
-
class Foo {
|
192
|
-
constructor(public readonly name: string) {}
|
193
|
-
}
|
194
|
-
```
|
195
|
-
|
196
|
-
</TabItem>
|
197
|
-
<TabItem value="✅ Correct">
|
198
|
-
|
199
|
-
```ts option='{ "allow": ["private"] }'
|
200
|
-
class Foo {
|
201
|
-
constructor(name: string) {}
|
202
|
-
}
|
203
|
-
|
204
|
-
class Foo {
|
205
|
-
constructor(private name: string) {}
|
206
|
-
}
|
207
|
-
```
|
208
|
-
|
209
|
-
</TabItem>
|
210
|
-
</Tabs>
|
211
|
-
|
212
|
-
### protected
|
213
|
-
|
214
|
-
Examples of code for the `{ "allow": ["protected"] }` options:
|
215
|
-
|
216
|
-
<Tabs>
|
217
|
-
<TabItem value="❌ Incorrect">
|
218
|
-
|
219
|
-
```ts option='{ "allow": ["protected"] }'
|
220
|
-
class Foo {
|
221
|
-
constructor(readonly name: string) {}
|
222
|
-
}
|
223
|
-
|
224
|
-
class Foo {
|
225
|
-
constructor(private name: string) {}
|
226
|
-
}
|
227
|
-
|
228
|
-
class Foo {
|
229
|
-
constructor(public name: string) {}
|
230
|
-
}
|
231
|
-
|
232
|
-
class Foo {
|
233
|
-
constructor(private readonly name: string) {}
|
234
|
-
}
|
235
|
-
|
236
|
-
class Foo {
|
237
|
-
constructor(protected readonly name: string) {}
|
238
|
-
}
|
239
|
-
|
240
|
-
class Foo {
|
241
|
-
constructor(public readonly name: string) {}
|
242
|
-
}
|
243
|
-
```
|
244
|
-
|
245
|
-
</TabItem>
|
246
|
-
<TabItem value="✅ Correct">
|
247
|
-
|
248
|
-
```ts option='{ "allow": ["protected"] }'
|
249
|
-
class Foo {
|
250
|
-
constructor(name: string) {}
|
251
|
-
}
|
252
|
-
|
253
|
-
class Foo {
|
254
|
-
constructor(protected name: string) {}
|
255
|
-
}
|
256
|
-
```
|
257
|
-
|
258
|
-
</TabItem>
|
259
|
-
</Tabs>
|
260
|
-
|
261
|
-
### public
|
262
|
-
|
263
|
-
Examples of code for the `{ "allow": ["public"] }` options:
|
264
|
-
|
265
|
-
<Tabs>
|
266
|
-
<TabItem value="❌ Incorrect">
|
267
|
-
|
268
|
-
```ts option='{ "allow": ["public"] }'
|
269
|
-
class Foo {
|
270
|
-
constructor(readonly name: string) {}
|
271
|
-
}
|
272
|
-
|
273
|
-
class Foo {
|
274
|
-
constructor(private name: string) {}
|
275
|
-
}
|
276
|
-
|
277
|
-
class Foo {
|
278
|
-
constructor(protected name: string) {}
|
279
|
-
}
|
280
|
-
|
281
|
-
class Foo {
|
282
|
-
constructor(private readonly name: string) {}
|
283
|
-
}
|
284
|
-
|
285
|
-
class Foo {
|
286
|
-
constructor(protected readonly name: string) {}
|
287
|
-
}
|
288
|
-
|
289
|
-
class Foo {
|
290
|
-
constructor(public readonly name: string) {}
|
291
|
-
}
|
292
|
-
```
|
293
|
-
|
294
|
-
</TabItem>
|
295
|
-
<TabItem value="✅ Correct">
|
296
|
-
|
297
|
-
```ts option='{ "allow": ["public"] }'
|
298
|
-
class Foo {
|
299
|
-
constructor(name: string) {}
|
300
|
-
}
|
301
|
-
|
302
|
-
class Foo {
|
303
|
-
constructor(public name: string) {}
|
304
|
-
}
|
305
|
-
```
|
306
|
-
|
307
|
-
</TabItem>
|
308
|
-
</Tabs>
|
309
|
-
|
310
|
-
### private readonly
|
311
|
-
|
312
|
-
Examples of code for the `{ "allow": ["private readonly"] }` options:
|
313
|
-
|
314
|
-
<Tabs>
|
315
|
-
<TabItem value="❌ Incorrect">
|
316
|
-
|
317
|
-
```ts option='{ "allow": ["private readonly"] }'
|
318
|
-
class Foo {
|
319
|
-
constructor(readonly name: string) {}
|
320
|
-
}
|
321
|
-
|
322
|
-
class Foo {
|
323
|
-
constructor(private name: string) {}
|
324
|
-
}
|
325
|
-
|
326
|
-
class Foo {
|
327
|
-
constructor(protected name: string) {}
|
328
|
-
}
|
329
|
-
|
330
|
-
class Foo {
|
331
|
-
constructor(public name: string) {}
|
332
|
-
}
|
333
|
-
|
334
|
-
class Foo {
|
335
|
-
constructor(protected readonly name: string) {}
|
336
|
-
}
|
337
|
-
|
338
|
-
class Foo {
|
339
|
-
constructor(public readonly name: string) {}
|
340
|
-
}
|
341
|
-
```
|
342
|
-
|
343
|
-
</TabItem>
|
344
|
-
<TabItem value="✅ Correct">
|
345
|
-
|
346
|
-
```ts option='{ "allow": ["private readonly"] }'
|
347
|
-
class Foo {
|
348
|
-
constructor(name: string) {}
|
349
|
-
}
|
350
|
-
|
351
|
-
class Foo {
|
352
|
-
constructor(private readonly name: string) {}
|
353
|
-
}
|
354
|
-
```
|
355
|
-
|
356
|
-
</TabItem>
|
357
|
-
</Tabs>
|
358
|
-
|
359
|
-
### protected readonly
|
360
|
-
|
361
|
-
Examples of code for the `{ "allow": ["protected readonly"] }` options:
|
362
|
-
|
363
|
-
<Tabs>
|
364
|
-
<TabItem value="❌ Incorrect">
|
365
|
-
|
366
|
-
```ts option='{ "allow": ["protected readonly"] }'
|
367
|
-
class Foo {
|
368
|
-
constructor(readonly name: string) {}
|
369
|
-
}
|
370
|
-
|
371
|
-
class Foo {
|
372
|
-
constructor(private name: string) {}
|
373
|
-
}
|
374
|
-
|
375
|
-
class Foo {
|
376
|
-
constructor(protected name: string) {}
|
377
|
-
}
|
378
|
-
|
379
|
-
class Foo {
|
380
|
-
constructor(public name: string) {}
|
381
|
-
}
|
382
|
-
|
383
|
-
class Foo {
|
384
|
-
constructor(private readonly name: string) {}
|
385
|
-
}
|
386
|
-
|
387
|
-
class Foo {
|
388
|
-
constructor(public readonly name: string) {}
|
389
|
-
}
|
390
|
-
```
|
391
|
-
|
392
|
-
</TabItem>
|
393
|
-
<TabItem value="✅ Correct">
|
394
|
-
|
395
|
-
```ts option='{ "allow": ["protected readonly"] }'
|
396
|
-
class Foo {
|
397
|
-
constructor(name: string) {}
|
398
|
-
}
|
399
|
-
|
400
|
-
class Foo {
|
401
|
-
constructor(protected readonly name: string) {}
|
402
|
-
}
|
403
|
-
```
|
404
|
-
|
405
|
-
</TabItem>
|
406
|
-
</Tabs>
|
407
|
-
|
408
|
-
### public readonly
|
409
|
-
|
410
|
-
Examples of code for the `{ "allow": ["public readonly"] }` options:
|
411
|
-
|
412
|
-
<Tabs>
|
413
|
-
<TabItem value="❌ Incorrect">
|
414
|
-
|
415
|
-
```ts option='{ "allow": ["public readonly"] }'
|
416
|
-
class Foo {
|
417
|
-
constructor(readonly name: string) {}
|
418
|
-
}
|
419
|
-
|
420
|
-
class Foo {
|
421
|
-
constructor(private name: string) {}
|
422
|
-
}
|
423
|
-
|
424
|
-
class Foo {
|
425
|
-
constructor(protected name: string) {}
|
426
|
-
}
|
427
|
-
|
428
|
-
class Foo {
|
429
|
-
constructor(public name: string) {}
|
430
|
-
}
|
431
|
-
|
432
|
-
class Foo {
|
433
|
-
constructor(private readonly name: string) {}
|
434
|
-
}
|
435
|
-
|
436
|
-
class Foo {
|
437
|
-
constructor(protected readonly name: string) {}
|
438
|
-
}
|
439
|
-
```
|
440
|
-
|
441
|
-
</TabItem>
|
442
|
-
<TabItem value="✅ Correct">
|
443
|
-
|
444
|
-
```ts option='{ "allow": ["public readonly"] }'
|
445
|
-
class Foo {
|
446
|
-
constructor(name: string) {}
|
447
|
-
}
|
448
|
-
|
449
|
-
class Foo {
|
450
|
-
constructor(public readonly name: string) {}
|
451
|
-
}
|
452
|
-
```
|
453
|
-
|
454
|
-
</TabItem>
|
455
|
-
</Tabs>
|
456
|
-
|
457
|
-
### `"parameter-property"`
|
458
|
-
|
459
|
-
Examples of code for the `{ "prefer": "parameter-property" }` option:
|
460
|
-
|
461
|
-
<Tabs>
|
462
|
-
<TabItem value="❌ Incorrect">
|
463
|
-
|
464
|
-
```ts option='{ "prefer": "parameter-property" }'
|
465
|
-
class Foo {
|
466
|
-
private name: string;
|
467
|
-
constructor(name: string) {
|
468
|
-
this.name = name;
|
469
|
-
}
|
470
|
-
}
|
471
|
-
|
472
|
-
class Foo {
|
473
|
-
public readonly name: string;
|
474
|
-
constructor(name: string) {
|
475
|
-
this.name = name;
|
476
|
-
}
|
477
|
-
}
|
478
|
-
|
479
|
-
class Foo {
|
480
|
-
constructor(name: string) {
|
481
|
-
this.name = name;
|
482
|
-
}
|
483
|
-
name: string;
|
484
|
-
}
|
485
|
-
```
|
486
|
-
|
487
|
-
</TabItem>
|
488
|
-
<TabItem value="✅ Correct">
|
489
|
-
|
490
|
-
```ts option='{ "prefer": "parameter-property" }'
|
491
|
-
class Foo {
|
492
|
-
private differentName: string;
|
493
|
-
constructor(name: string) {
|
494
|
-
this.differentName = name;
|
495
|
-
}
|
496
|
-
}
|
497
|
-
|
498
|
-
class Foo {
|
499
|
-
private differentType: number | undefined;
|
500
|
-
constructor(differentType: number) {
|
501
|
-
this.differentType = differentType;
|
502
|
-
}
|
503
|
-
}
|
504
|
-
|
505
|
-
class Foo {
|
506
|
-
protected logicInConstructor: string;
|
507
|
-
constructor(logicInConstructor: string) {
|
508
|
-
console.log('Hello, world!');
|
509
|
-
this.logicInConstructor = logicInConstructor;
|
510
|
-
}
|
511
|
-
}
|
512
|
-
```
|
513
|
-
|
514
|
-
</TabItem>
|
515
|
-
</Tabs>
|
516
|
-
|
517
|
-
## When Not To Use It
|
518
|
-
|
519
|
-
If you don't care about which style of parameter properties in constructors is used in your classes, then you will not need this rule.
|
520
|
-
|
521
|
-
However, keep in mind that inconsistent style can harm readability in a project.
|
522
|
-
We recommend picking a single option for this rule that works best for your project.
|
@@ -1,51 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Enforce the use of `as const` over literal 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/prefer-as-const** for documentation.
|
11
|
-
|
12
|
-
There are two common ways to tell TypeScript that a literal value should be interpreted as its literal type (e.g. `2`) rather than general primitive type (e.g. `number`);
|
13
|
-
|
14
|
-
- `as const`: telling TypeScript to infer the literal type automatically
|
15
|
-
- `as` with the literal type: explicitly telling the literal type to TypeScript
|
16
|
-
|
17
|
-
`as const` is generally preferred, as it doesn't require re-typing the literal value.
|
18
|
-
This rule reports when an `as` with an explicit literal type can be replaced with an `as const`.
|
19
|
-
|
20
|
-
## Examples
|
21
|
-
|
22
|
-
<Tabs>
|
23
|
-
<TabItem value="❌ Incorrect">
|
24
|
-
|
25
|
-
```ts
|
26
|
-
let bar: 2 = 2;
|
27
|
-
let foo = <'bar'>'bar';
|
28
|
-
let foo = { bar: 'baz' as 'baz' };
|
29
|
-
```
|
30
|
-
|
31
|
-
</TabItem>
|
32
|
-
<TabItem value="✅ Correct">
|
33
|
-
|
34
|
-
```ts
|
35
|
-
let foo = 'bar';
|
36
|
-
let foo = 'bar' as const;
|
37
|
-
let foo: 'bar' = 'bar' as const;
|
38
|
-
let bar = 'bar' as string;
|
39
|
-
let foo = <string>'bar';
|
40
|
-
let foo = { bar: 'baz' };
|
41
|
-
```
|
42
|
-
|
43
|
-
</TabItem>
|
44
|
-
</Tabs>
|
45
|
-
|
46
|
-
## When Not To Use It
|
47
|
-
|
48
|
-
If you don't care about which style of literals assertions is used in your code, then you will not need this rule.
|
49
|
-
|
50
|
-
However, keep in mind that inconsistent style can harm readability in a project.
|
51
|
-
We recommend picking a single option for this rule that works best for your project.
|
@@ -1,101 +0,0 @@
|
|
1
|
-
---
|
2
|
-
description: 'Require destructuring from arrays and/or objects.'
|
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-destructuring** for documentation.
|
11
|
-
|
12
|
-
It adds support for TypeScript's type annotations in variable declarations.
|
13
|
-
|
14
|
-
## Examples
|
15
|
-
|
16
|
-
<Tabs>
|
17
|
-
|
18
|
-
<TabItem value="`eslint/prefer-destructuring`">
|
19
|
-
|
20
|
-
```ts
|
21
|
-
const x: string = obj.x; // This is incorrect and the auto fixer provides following untyped fix.
|
22
|
-
// const { x } = obj;
|
23
|
-
```
|
24
|
-
|
25
|
-
</TabItem>
|
26
|
-
<TabItem value="`@typescript-eslint/prefer-destructuring`">
|
27
|
-
|
28
|
-
```ts
|
29
|
-
const x: string = obj.x; // This is correct by default. You can also forbid this by an option.
|
30
|
-
```
|
31
|
-
|
32
|
-
</TabItem>
|
33
|
-
</Tabs>
|
34
|
-
|
35
|
-
And it infers binding patterns more accurately thanks to the type checker.
|
36
|
-
|
37
|
-
<Tabs>
|
38
|
-
<TabItem value="❌ Incorrect">
|
39
|
-
|
40
|
-
```ts
|
41
|
-
const x = ['a'];
|
42
|
-
const y = x[0];
|
43
|
-
```
|
44
|
-
|
45
|
-
</TabItem>
|
46
|
-
<TabItem value="✅ Correct">
|
47
|
-
|
48
|
-
```ts
|
49
|
-
const x = { 0: 'a' };
|
50
|
-
const y = x[0];
|
51
|
-
```
|
52
|
-
|
53
|
-
It is correct when `enforceForRenamedProperties` is not true.
|
54
|
-
Valid destructuring syntax is renamed style like `{ 0: y } = x` rather than `[y] = x` because `x` is not iterable.
|
55
|
-
|
56
|
-
</TabItem>
|
57
|
-
</Tabs>
|
58
|
-
|
59
|
-
## Options
|
60
|
-
|
61
|
-
This rule adds the following options:
|
62
|
-
|
63
|
-
```ts
|
64
|
-
type Options = [
|
65
|
-
BasePreferDestructuringOptions[0],
|
66
|
-
BasePreferDestructuringOptions[1] & {
|
67
|
-
enforceForDeclarationWithTypeAnnotation?: boolean;
|
68
|
-
},
|
69
|
-
];
|
70
|
-
|
71
|
-
const defaultOptions: Options = [
|
72
|
-
basePreferDestructuringDefaultOptions[0],
|
73
|
-
{
|
74
|
-
...basePreferDestructuringDefaultOptions[1],
|
75
|
-
enforceForDeclarationWithTypeAnnotation: false,
|
76
|
-
},
|
77
|
-
];
|
78
|
-
```
|
79
|
-
|
80
|
-
### `enforceForDeclarationWithTypeAnnotation`
|
81
|
-
|
82
|
-
{/* insert option description */}
|
83
|
-
|
84
|
-
Examples with `{ enforceForDeclarationWithTypeAnnotation: true }`:
|
85
|
-
|
86
|
-
<Tabs>
|
87
|
-
<TabItem value="❌ Incorrect">
|
88
|
-
|
89
|
-
```ts option='{ "object": true }, { "enforceForDeclarationWithTypeAnnotation": true }'
|
90
|
-
const x: string = obj.x;
|
91
|
-
```
|
92
|
-
|
93
|
-
</TabItem>
|
94
|
-
<TabItem value="✅ Correct">
|
95
|
-
|
96
|
-
```ts option='{ "object": true }, { "enforceForDeclarationWithTypeAnnotation": true }'
|
97
|
-
const { x }: { x: string } = obj;
|
98
|
-
```
|
99
|
-
|
100
|
-
</TabItem>
|
101
|
-
</Tabs>
|