@typescript-eslint/eslint-plugin 8.21.1-alpha.0 → 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.
Files changed (32) hide show
  1. package/dist/rules/no-duplicate-type-constituents.js +97 -71
  2. package/dist/rules/no-duplicate-type-constituents.js.map +1 -1
  3. package/dist/rules/no-extraneous-class.js +4 -3
  4. package/dist/rules/no-extraneous-class.js.map +1 -1
  5. package/dist/rules/no-shadow.js +2 -3
  6. package/dist/rules/no-shadow.js.map +1 -1
  7. package/dist/rules/no-unnecessary-condition.js +7 -43
  8. package/dist/rules/no-unnecessary-condition.js.map +1 -1
  9. package/dist/rules/no-unnecessary-template-expression.js +257 -173
  10. package/dist/rules/no-unnecessary-template-expression.js.map +1 -1
  11. package/dist/rules/prefer-nullish-coalescing.js +106 -56
  12. package/dist/rules/prefer-nullish-coalescing.js.map +1 -1
  13. package/dist/rules/prefer-readonly.js +58 -1
  14. package/dist/rules/prefer-readonly.js.map +1 -1
  15. package/dist/util/getValueOfLiteralType.js +17 -0
  16. package/dist/util/getValueOfLiteralType.js.map +1 -0
  17. package/dist/util/index.js +2 -0
  18. package/dist/util/index.js.map +1 -1
  19. package/dist/util/truthinessAndNullishUtils.js +68 -0
  20. package/dist/util/truthinessAndNullishUtils.js.map +1 -0
  21. package/docs/rules/no-explicit-any.mdx +1 -0
  22. package/docs/rules/no-unnecessary-template-expression.mdx +19 -0
  23. package/docs/rules/no-unsafe-argument.mdx +1 -0
  24. package/docs/rules/no-unsafe-assignment.mdx +1 -0
  25. package/docs/rules/no-unsafe-call.mdx +1 -0
  26. package/docs/rules/no-unsafe-function-type.mdx +1 -0
  27. package/docs/rules/no-unsafe-member-access.mdx +1 -0
  28. package/docs/rules/no-unsafe-return.mdx +1 -0
  29. package/docs/rules/no-unused-vars.mdx +68 -1
  30. package/docs/rules/prefer-nullish-coalescing.mdx +5 -0
  31. package/docs/rules/use-unknown-in-catch-callback-variable.mdx +4 -0
  32. package/package.json +7 -7
@@ -93,6 +93,7 @@ You might consider using [ESLint disable comments](https://eslint.org/docs/lates
93
93
 
94
94
  ## Related To
95
95
 
96
+ - [Avoiding `any`s with Linting and TypeScript](/blog/avoiding-anys)
96
97
  - [`no-explicit-any`](./no-explicit-any.mdx)
97
98
  - [`no-unsafe-argument`](./no-unsafe-argument.mdx)
98
99
  - [`no-unsafe-call`](./no-unsafe-call.mdx)
@@ -112,6 +112,7 @@ You might consider using [ESLint disable comments](https://eslint.org/docs/lates
112
112
 
113
113
  ## Related To
114
114
 
115
+ - [Avoiding `any`s with Linting and TypeScript](/blog/avoiding-anys)
115
116
  - [`no-explicit-any`](./no-explicit-any.mdx)
116
117
  - [`no-unsafe-argument`](./no-unsafe-argument.mdx)
117
118
  - [`no-unsafe-assignment`](./no-unsafe-assignment.mdx)
@@ -58,6 +58,7 @@ You might consider using [ESLint disable comments](https://eslint.org/docs/lates
58
58
 
59
59
  ## Related To
60
60
 
61
+ - [Avoiding `any`s with Linting and TypeScript](/blog/avoiding-anys)
61
62
  - [`no-empty-object-type`](./no-empty-object-type.mdx)
62
63
  - [`no-restricted-types`](./no-restricted-types.mdx)
63
64
  - [`no-unsafe-call`](./no-unsafe-call.mdx)
@@ -73,6 +73,7 @@ You might consider using [ESLint disable comments](https://eslint.org/docs/lates
73
73
 
74
74
  ## Related To
75
75
 
76
+ - [Avoiding `any`s with Linting and TypeScript](/blog/avoiding-anys)
76
77
  - [`no-explicit-any`](./no-explicit-any.mdx)
77
78
  - [`no-unsafe-argument`](./no-unsafe-argument.mdx)
78
79
  - [`no-unsafe-assignment`](./no-unsafe-assignment.mdx)
@@ -118,6 +118,7 @@ You might consider using [ESLint disable comments](https://eslint.org/docs/lates
118
118
 
119
119
  ## Related To
120
120
 
121
+ - [Avoiding `any`s with Linting and TypeScript](/blog/avoiding-anys)
121
122
  - [`no-explicit-any`](./no-explicit-any.mdx)
122
123
  - [`no-unsafe-argument`](./no-unsafe-argument.mdx)
123
124
  - [`no-unsafe-assignment`](./no-unsafe-assignment.mdx)
@@ -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
- ## Benefits Over TypeScript
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`
@@ -91,3 +91,7 @@ For further reading on this, you may also want to look into
91
91
  If your codebase is not yet able to enable `useUnknownInCatchVariables`, it likely would be similarly difficult to enable this rule.
92
92
 
93
93
  If you have modified the global type declarations in order to make `then()` and `catch()` callbacks use the `unknown` type without an explicit type annotation, you do not need this rule.
94
+
95
+ ## Related To
96
+
97
+ - [Avoiding `any`s with Linting and TypeScript](/blog/avoiding-anys)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typescript-eslint/eslint-plugin",
3
- "version": "8.21.1-alpha.0",
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.0",
65
- "@typescript-eslint/type-utils": "8.21.1-alpha.0",
66
- "@typescript-eslint/utils": "8.21.1-alpha.0",
67
- "@typescript-eslint/visitor-keys": "8.21.1-alpha.0",
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.0",
79
- "@typescript-eslint/rule-tester": "8.21.1-alpha.0",
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": "*",