@typescript-eslint/eslint-plugin 8.21.1-alpha.1 → 8.21.1-alpha.10
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/dist/rules/no-duplicate-type-constituents.js +97 -71
- package/dist/rules/no-duplicate-type-constituents.js.map +1 -1
- package/dist/rules/no-extraneous-class.js +4 -3
- package/dist/rules/no-extraneous-class.js.map +1 -1
- package/dist/rules/no-shadow.js +2 -3
- package/dist/rules/no-shadow.js.map +1 -1
- package/dist/rules/no-unnecessary-condition.js +7 -43
- package/dist/rules/no-unnecessary-condition.js.map +1 -1
- package/dist/rules/no-unnecessary-template-expression.js +257 -173
- package/dist/rules/no-unnecessary-template-expression.js.map +1 -1
- package/dist/rules/prefer-nullish-coalescing.js +106 -56
- package/dist/rules/prefer-nullish-coalescing.js.map +1 -1
- package/dist/rules/prefer-readonly.js +58 -1
- package/dist/rules/prefer-readonly.js.map +1 -1
- package/dist/util/getValueOfLiteralType.js +17 -0
- package/dist/util/getValueOfLiteralType.js.map +1 -0
- package/dist/util/index.js +2 -0
- package/dist/util/index.js.map +1 -1
- package/dist/util/truthinessAndNullishUtils.js +68 -0
- package/dist/util/truthinessAndNullishUtils.js.map +1 -0
- package/docs/rules/no-unnecessary-template-expression.mdx +19 -0
- package/docs/rules/no-unused-vars.mdx +68 -1
- package/docs/rules/prefer-nullish-coalescing.mdx +5 -0
- package/package.json +7 -7
@@ -12,7 +12,11 @@ import TabItem from '@theme/TabItem';
|
|
12
12
|
This rule extends the base [`eslint/no-unused-vars`](https://eslint.org/docs/rules/no-unused-vars) rule.
|
13
13
|
It adds support for TypeScript features, such as types.
|
14
14
|
|
15
|
-
##
|
15
|
+
## Options
|
16
|
+
|
17
|
+
## FAQs
|
18
|
+
|
19
|
+
### What benefits does this rule have over TypeScript?
|
16
20
|
|
17
21
|
TypeScript provides [`noUnusedLocals`](https://www.typescriptlang.org/tsconfig#noUnusedLocals) and [`noUnusedParameters`](https://www.typescriptlang.org/tsconfig#noUnusedParameters) compiler options that can report errors on unused local variables or parameters, respectively.
|
18
22
|
Those compiler options can be convenient to use if you don't want to set up ESLint and typescript-eslint.
|
@@ -52,3 +56,66 @@ Also see similar rules provided by ESLint:
|
|
52
56
|
|
53
57
|
- [`no-unused-private-class-members`](https://eslint.org/docs/latest/rules/no-unused-private-class-members)
|
54
58
|
- [`no-unused-labels`](https://eslint.org/docs/latest/rules/no-unused-labels)
|
59
|
+
|
60
|
+
### Why does this rule report variables used only for types?
|
61
|
+
|
62
|
+
This rule does not count type-only uses when determining whether a variable is used.
|
63
|
+
Declaring variables only to use them for types adds code and runtime complexity.
|
64
|
+
The variables are never actually used at runtime.
|
65
|
+
They can be misleading to readers of the code.
|
66
|
+
|
67
|
+
<Tabs>
|
68
|
+
|
69
|
+
<TabItem value="typeof Variables">
|
70
|
+
|
71
|
+
For example, if a variable is only used for `typeof`, this rule will report:
|
72
|
+
|
73
|
+
```ts
|
74
|
+
const box = {
|
75
|
+
// ~~~
|
76
|
+
// 'box' is assigned a value but only used as a type.
|
77
|
+
value: 123,
|
78
|
+
};
|
79
|
+
|
80
|
+
export type Box = typeof box;
|
81
|
+
```
|
82
|
+
|
83
|
+
Instead, it's often cleaner and less code to write out the types directly:
|
84
|
+
|
85
|
+
```ts
|
86
|
+
export interface Box {
|
87
|
+
value: number;
|
88
|
+
}
|
89
|
+
```
|
90
|
+
|
91
|
+
</TabItem>
|
92
|
+
|
93
|
+
<TabItem value="Zod Schemas">
|
94
|
+
|
95
|
+
For example, if a Zod schema variable is only used for `typeof`, this rule will report:
|
96
|
+
|
97
|
+
```ts
|
98
|
+
import { z } from 'zod';
|
99
|
+
|
100
|
+
const schema = z.object({
|
101
|
+
// ~~~~~~
|
102
|
+
// 'schema' is assigned a value but only used as a type.
|
103
|
+
value: z.number(),
|
104
|
+
});
|
105
|
+
|
106
|
+
export type Box = z.infer<typeof schema>;
|
107
|
+
```
|
108
|
+
|
109
|
+
Instead, it's often cleaner and less code to write out the types directly:
|
110
|
+
|
111
|
+
```ts
|
112
|
+
export interface Box {
|
113
|
+
value: number;
|
114
|
+
}
|
115
|
+
```
|
116
|
+
|
117
|
+
</TabItem>
|
118
|
+
|
119
|
+
</Tabs>
|
120
|
+
|
121
|
+
If you find yourself writing runtime values only for types, consider refactoring your code to declare types directly.
|
@@ -16,6 +16,7 @@ This rule reports when you may consider replacing:
|
|
16
16
|
|
17
17
|
- An `||` operator with `??`
|
18
18
|
- An `||=` operator with `??=`
|
19
|
+
- Ternary expressions (`?:`) that are equivalent to `||` or `??` with `??`
|
19
20
|
|
20
21
|
:::caution
|
21
22
|
This rule will not work as expected if [`strictNullChecks`](https://www.typescriptlang.org/tsconfig#strictNullChecks) is not enabled.
|
@@ -42,7 +43,9 @@ foo === undefined ? 'a string' : foo;
|
|
42
43
|
|
43
44
|
const foo: string | null = 'bar';
|
44
45
|
foo !== null ? foo : 'a string';
|
46
|
+
foo ? foo : 'a string';
|
45
47
|
foo === null ? 'a string' : foo;
|
48
|
+
!foo ? 'a string' : foo;
|
46
49
|
```
|
47
50
|
|
48
51
|
Correct code for `ignoreTernaryTests: false`:
|
@@ -61,6 +64,8 @@ foo ?? 'a string';
|
|
61
64
|
const foo: string | null = 'bar';
|
62
65
|
foo ?? 'a string';
|
63
66
|
foo ?? 'a string';
|
67
|
+
foo ?? 'a string';
|
68
|
+
foo ?? 'a string';
|
64
69
|
```
|
65
70
|
|
66
71
|
### `ignoreConditionalTests`
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@typescript-eslint/eslint-plugin",
|
3
|
-
"version": "8.21.1-alpha.
|
3
|
+
"version": "8.21.1-alpha.10",
|
4
4
|
"description": "TypeScript plugin for ESLint",
|
5
5
|
"files": [
|
6
6
|
"dist",
|
@@ -61,10 +61,10 @@
|
|
61
61
|
},
|
62
62
|
"dependencies": {
|
63
63
|
"@eslint-community/regexpp": "^4.10.0",
|
64
|
-
"@typescript-eslint/scope-manager": "8.21.1-alpha.
|
65
|
-
"@typescript-eslint/type-utils": "8.21.1-alpha.
|
66
|
-
"@typescript-eslint/utils": "8.21.1-alpha.
|
67
|
-
"@typescript-eslint/visitor-keys": "8.21.1-alpha.
|
64
|
+
"@typescript-eslint/scope-manager": "8.21.1-alpha.10",
|
65
|
+
"@typescript-eslint/type-utils": "8.21.1-alpha.10",
|
66
|
+
"@typescript-eslint/utils": "8.21.1-alpha.10",
|
67
|
+
"@typescript-eslint/visitor-keys": "8.21.1-alpha.10",
|
68
68
|
"graphemer": "^1.4.0",
|
69
69
|
"ignore": "^5.3.1",
|
70
70
|
"natural-compare": "^1.4.0",
|
@@ -75,8 +75,8 @@
|
|
75
75
|
"@types/marked": "^5.0.2",
|
76
76
|
"@types/mdast": "^4.0.3",
|
77
77
|
"@types/natural-compare": "*",
|
78
|
-
"@typescript-eslint/rule-schema-to-typescript-types": "8.21.1-alpha.
|
79
|
-
"@typescript-eslint/rule-tester": "8.21.1-alpha.
|
78
|
+
"@typescript-eslint/rule-schema-to-typescript-types": "8.21.1-alpha.10",
|
79
|
+
"@typescript-eslint/rule-tester": "8.21.1-alpha.10",
|
80
80
|
"ajv": "^6.12.6",
|
81
81
|
"cross-env": "^7.0.3",
|
82
82
|
"cross-fetch": "*",
|