@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,85 +0,0 @@
1
- ---
2
- description: 'Disallow type arguments that are equal to the default.'
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-unnecessary-type-arguments** for documentation.
11
-
12
- Type parameters in TypeScript may specify a default value.
13
- For example:
14
-
15
- ```ts
16
- function f<T = number>(/* ... */) {
17
- // ...
18
- }
19
- ```
20
-
21
- It is redundant to provide an explicit type parameter equal to that default: e.g. calling `f<number>(...)`.
22
- This rule reports when an explicitly specified type argument is the default for that type parameter.
23
-
24
- ## Examples
25
-
26
- <Tabs>
27
- <TabItem value="❌ Incorrect">
28
-
29
- ```ts
30
- function f<T = number>() {}
31
- f<number>();
32
- ```
33
-
34
- ```ts
35
- function g<T = number, U = string>() {}
36
- g<string, string>();
37
- ```
38
-
39
- ```ts
40
- class C<T = number> {}
41
- new C<number>();
42
-
43
- class D extends C<number> {}
44
- ```
45
-
46
- ```ts
47
- interface I<T = number> {}
48
- class Impl implements I<number> {}
49
- ```
50
-
51
- </TabItem>
52
- <TabItem value="✅ Correct">
53
-
54
- ```ts
55
- function f<T = number>() {}
56
- f();
57
- f<string>();
58
- ```
59
-
60
- ```ts
61
- function g<T = number, U = string>() {}
62
- g<string>();
63
- g<number, number>();
64
- ```
65
-
66
- ```ts
67
- class C<T = number> {}
68
- new C();
69
- new C<string>();
70
-
71
- class D extends C {}
72
- class D extends C<string> {}
73
- ```
74
-
75
- ```ts
76
- interface I<T = number> {}
77
- class Impl implements I<string> {}
78
- ```
79
-
80
- </TabItem>
81
- </Tabs>
82
-
83
- ## When Not To Use It
84
-
85
- If you prefer explicitly specifying type parameters even when they are equal to the default, you can skip this rule.
@@ -1,97 +0,0 @@
1
- ---
2
- description: 'Disallow type assertions that do not change the type of an expression.'
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-unnecessary-type-assertion** for documentation.
11
-
12
- TypeScript can be told an expression is a different type than expected using `as` type assertions.
13
- Leaving `as` assertions in the codebase increases visual clutter and harms code readability, so it's generally best practice to remove them if they don't change the type of an expression.
14
- This rule reports when a type assertion does not change the type of an expression.
15
-
16
- ## Examples
17
-
18
- <Tabs>
19
- <TabItem value="❌ Incorrect">
20
-
21
- ```ts
22
- const foo = 3;
23
- const bar = foo!;
24
- ```
25
-
26
- ```ts
27
- const foo = <number>(3 + 5);
28
- ```
29
-
30
- ```ts
31
- type Foo = number;
32
- const foo = <Foo>(3 + 5);
33
- ```
34
-
35
- ```ts
36
- type Foo = number;
37
- const foo = (3 + 5) as Foo;
38
- ```
39
-
40
- ```ts
41
- function foo(x: number): number {
42
- return x!; // unnecessary non-null
43
- }
44
- ```
45
-
46
- </TabItem>
47
- <TabItem value="✅ Correct">
48
-
49
- ```ts
50
- const foo = <number>3;
51
- ```
52
-
53
- ```ts
54
- const foo = 3 as number;
55
- ```
56
-
57
- ```ts
58
- let foo = 'foo' as const;
59
- ```
60
-
61
- ```ts
62
- function foo(x: number | undefined): number {
63
- return x!;
64
- }
65
- ```
66
-
67
- </TabItem>
68
- </Tabs>
69
-
70
- ## Options
71
-
72
- ### `checkLiteralConstAssertions`
73
-
74
- {/* insert option description */}
75
-
76
- With `@typescript-eslint/no-unnecessary-type-assertion: ["error", { checkLiteralConstAssertions: true }]`, the following is **incorrect** code:
77
-
78
- ```ts option='{ "checkLiteralConstAssertions": true }' showPlaygroundButton
79
- const foo = 'foo' as const;
80
- ```
81
-
82
- See [#8721 False positives for "as const" assertions (issue comment)](https://github.com/typescript-eslint/typescript-eslint/issues/8721#issuecomment-2145291966) for more information on this option.
83
-
84
- ### `typesToIgnore`
85
-
86
- {/* insert option description */}
87
-
88
- With `@typescript-eslint/no-unnecessary-type-assertion: ["error", { typesToIgnore: ['Foo'] }]`, the following is **correct** code:
89
-
90
- ```ts option='{ "typesToIgnore": ["Foo"] }' showPlaygroundButton
91
- type Foo = 3;
92
- const foo: Foo = 3;
93
- ```
94
-
95
- ## When Not To Use It
96
-
97
- If you don't care about having no-op type assertions in your code, then you can turn off this rule.
@@ -1,61 +0,0 @@
1
- ---
2
- description: 'Disallow unnecessary constraints on generic types.'
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-unnecessary-type-constraint** for documentation.
11
-
12
- Generic type parameters (`<T>`) in TypeScript may be "constrained" with an [`extends` keyword](https://www.typescriptlang.org/docs/handbook/generics.html#generic-constraints).
13
- When no `extends` is provided, type parameters default a constraint to `unknown`.
14
- It is therefore redundant to `extend` from `any` or `unknown`.
15
-
16
- ## Examples
17
-
18
- <Tabs>
19
- <TabItem value="❌ Incorrect">
20
-
21
- ```ts
22
- interface FooAny<T extends any> {}
23
-
24
- interface FooUnknown<T extends unknown> {}
25
-
26
- type BarAny<T extends any> = {};
27
-
28
- type BarUnknown<T extends unknown> = {};
29
-
30
- class BazAny<T extends any> {
31
- quxAny<U extends any>() {}
32
- }
33
-
34
- const QuuxAny = <T extends any>() => {};
35
-
36
- function QuuzAny<T extends any>() {}
37
- ```
38
-
39
- </TabItem>
40
- <TabItem value="✅ Correct">
41
-
42
- ```ts
43
- interface Foo<T> {}
44
-
45
- type Bar<T> = {};
46
-
47
- class Baz<T> {
48
- qux<U>() {}
49
- }
50
-
51
- const Quux = <T>() => {};
52
-
53
- function Quuz<T>() {}
54
- ```
55
-
56
- </TabItem>
57
- </Tabs>
58
-
59
- ## When Not To Use It
60
-
61
- If you don't care about the specific styles of your type constraints, or never use them in the first place, then you will not need this rule.
@@ -1,79 +0,0 @@
1
- ---
2
- description: 'Disallow conversion idioms when they do not change the type or value of the expression.'
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-unnecessary-type-conversion** for documentation.
11
-
12
- JavaScript provides several commonly used idioms to convert values to a specific type:
13
-
14
- - Primitive coercion (e.g. `Boolean(value)`, `String(value)`): using a built-in primitive function
15
- - String concatenation (e.g. `value + ''`): turning a value into a string
16
- - Unary coercion (e.g. `+value`, `!!value`): using a built-in operator
17
- - The `.toString()` method defined on many types
18
-
19
- These conversions are unnecessary if the value is already of that type.
20
-
21
- ## Examples
22
-
23
- <Tabs>
24
- <TabItem value="❌ Incorrect">
25
-
26
- ```ts
27
- String('123');
28
- '123'.toString();
29
- '' + '123';
30
- '123' + '';
31
-
32
- Number(123);
33
- +123;
34
- ~~123;
35
-
36
- Boolean(true);
37
- !!true;
38
-
39
- BigInt(BigInt(1));
40
-
41
- let str = '123';
42
- str += '';
43
- ```
44
-
45
- </TabItem>
46
- <TabItem value="✅ Correct">
47
-
48
- ```ts
49
- function foo(bar: string | number) {
50
- String(bar);
51
- bar.toString();
52
- '' + bar;
53
- bar + '';
54
-
55
- Number(bar);
56
- +bar;
57
- ~~bar;
58
-
59
- Boolean(bar);
60
- !!bar;
61
-
62
- BigInt(1);
63
-
64
- bar += '';
65
- }
66
- ```
67
-
68
- </TabItem>
69
- </Tabs>
70
-
71
- ## When Not To Use It
72
-
73
- If you don't care about having no-op type conversions in your code, then you can turn off this rule.
74
- If you have types which are not accurate, then this rule might cause you to remove conversions that you actually do need.
75
-
76
- ## Related To
77
-
78
- - [no-unnecessary-type-assertion](./no-unnecessary-type-assertion.mdx)
79
- - [no-useless-template-literals](./no-useless-template-literals.mdx)
@@ -1,255 +0,0 @@
1
- ---
2
- description: "Disallow type parameters that aren't used multiple times."
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-unnecessary-type-parameters** for documentation.
11
-
12
- This rule forbids type parameters that aren't used multiple times in a function, method, or class definition.
13
-
14
- Type parameters relate two types.
15
- If a type parameter is only used once, then it is not relating anything.
16
- It can usually be replaced with explicit types such as `unknown`.
17
-
18
- At best unnecessary type parameters make code harder to read.
19
- At worst they can be used to disguise unsafe type assertions.
20
-
21
- :::warning
22
- This rule was recently added, and has a surprising amount of hidden complexity compared to most of our rules. If you encounter unexpected behavior with it, please check closely the [Limitations](#limitations) and [FAQ](#faq) sections below and our [issue tracker](https://github.com/typescript-eslint/typescript-eslint/issues?q=is%3Aissue+no-unnecessary-type-parameters).
23
- If you don't see your case covered, please [reach out to us](https://typescript-eslint.io/contributing/issues)!
24
- :::
25
-
26
- ## Examples
27
-
28
- <Tabs>
29
- <TabItem value="❌ Incorrect">
30
-
31
- ```ts
32
- function second<A, B>(a: A, b: B): B {
33
- return b;
34
- }
35
-
36
- function parseJSON<T>(input: string): T {
37
- return JSON.parse(input);
38
- }
39
-
40
- function printProperty<T, K extends keyof T>(obj: T, key: K) {
41
- console.log(obj[key]);
42
- }
43
- ```
44
-
45
- </TabItem>
46
- <TabItem value="✅ Correct">
47
-
48
- ```ts
49
- function second<B>(a: unknown, b: B): B {
50
- return b;
51
- }
52
-
53
- function parseJSON(input: string): unknown {
54
- return JSON.parse(input);
55
- }
56
-
57
- function printProperty<T>(obj: T, key: keyof T) {
58
- console.log(obj[key]);
59
- }
60
-
61
- // T appears twice: in the type of arg and as the return type
62
- function identity<T>(arg: T): T {
63
- return arg;
64
- }
65
-
66
- // T appears twice: "keyof T" and in the inferred return type (T[K]).
67
- // K appears twice: "key: K" and in the inferred return type (T[K]).
68
- function getProperty<T, K extends keyof T>(obj: T, key: K) {
69
- return obj[key];
70
- }
71
- ```
72
-
73
- </TabItem>
74
- </Tabs>
75
-
76
- ## Limitations
77
-
78
- Note that this rule allows any type parameter that is used multiple times, even if those uses are via a type argument.
79
- For example, the following `T` is used multiple times by virtue of being in an `Array`, even though its name only appears once after declaration:
80
-
81
- ```ts
82
- declare function createStateHistory<T>(): T[];
83
- ```
84
-
85
- This is because the type parameter `T` relates multiple methods in `T[]` (`Array<T>`) together, making it used more than once.
86
-
87
- Therefore, this rule won't report on type parameters used as a type argument.
88
- This includes type arguments provided to global types such as `Array`, `Map`, and `Set` that have multiple methods and properties that can change values based on the type parameter.
89
-
90
- On the other hand, readonly and fixed array-likes such as `readonly T[]`, `ReadonlyArray`, and tuples such as `[T]` are special cases that are specifically reported on when used as input types, or as `readonly` output types.
91
- The following example will be reported because `T` is used only once as type argument for the `ReadonlyArray` global type:
92
-
93
- <Tabs>
94
- <TabItem value="❌ Incorrect">
95
-
96
- ```ts
97
- declare function length<T>(array: ReadonlyArray<T>): number;
98
- ```
99
-
100
- </TabItem>
101
- <TabItem value="✅ Correct">
102
-
103
- ```ts
104
- declare function length(array: ReadonlyArray<unknown>): number;
105
- ```
106
-
107
- </TabItem>
108
- </Tabs>
109
-
110
- ## FAQ
111
-
112
- ### The return type is only used as an input, so why isn't the rule reporting?
113
-
114
- One common reason that this might be the case is when the return type is not specified explicitly.
115
- The rule uses uses type information to count implicit usages of the type parameter in the function signature, including in the inferred return type.
116
- For example, the following function...
117
-
118
- ```ts
119
- function identity<T>(arg: T) {
120
- return arg;
121
- }
122
- ```
123
-
124
- ...implicitly has a return type of `T`. Therefore, the type parameter `T` is used twice, and the rule will not report this function.
125
-
126
- For other reasons the rule might not be reporting, be sure to check the [Limitations section](#limitations) and other FAQs.
127
-
128
- ### I'm using the type parameter inside the function, so why is the rule reporting?
129
-
130
- You might be surprised to that the rule reports on a function like this:
131
-
132
- ```ts
133
- function log<T extends string>(string1: T): void {
134
- const string2: T = string1;
135
- console.log(string2);
136
- }
137
- ```
138
-
139
- After all, the type parameter `T` relates the input `string1` and the local variable `string2`, right?
140
- However, this usage is unnecessary, since we can achieve the same results by replacing all usages of the type parameter with its constraint.
141
- That is to say, the function can always be rewritten as:
142
-
143
- ```ts
144
- function log(string1: string): void {
145
- const string2: string = string1;
146
- console.log(string2);
147
- }
148
- ```
149
-
150
- Therefore, this rule only counts usages of a type parameter in the _signature_ of a function, method, or class, but not in the implementation. See also [#9735](https://github.com/typescript-eslint/typescript-eslint/issues/9735)
151
-
152
- ### Why am I getting TypeScript errors saying "Object literal may only specify known properties" after removing an unnecessary type parameter?
153
-
154
- Suppose you have a situation like the following, which will trigger the rule to report.
155
-
156
- ```ts
157
- interface SomeProperties {
158
- foo: string;
159
- }
160
-
161
- // T is only used once, so the rule will report.
162
- function serialize<T extends SomeProperties>(x: T): string {
163
- return JSON.stringify(x);
164
- }
165
-
166
- serialize({ foo: 'bar', anotherProperty: 'baz' });
167
- ```
168
-
169
- If we remove the unnecessary type parameter, we'll get an error:
170
-
171
- ```ts
172
- function serialize(x: SomeProperties): string {
173
- return JSON.stringify(x);
174
- }
175
-
176
- // TS Error: Object literal may only specify known properties, and 'anotherProperty' does not exist in type 'SomeProperties'.
177
- serialize({ foo: 'bar', anotherProperty: 'baz' });
178
- ```
179
-
180
- This is because TypeScript figures it's _usually_ an error to explicitly provide excess properties in a location that expects a specific type.
181
- See [the TypeScript handbook's section on excess property checks](https://www.typescriptlang.org/docs/handbook/2/objects.html#excess-property-checks) for further discussion.
182
-
183
- To resolve this, you have two approaches to choose from.
184
-
185
- 1. If it doesn't make sense to accept excess properties in your function, you'll want to fix the errors at the call sites. Usually, you can simply remove any excess properties where the function is called.
186
- 2. Otherwise, if you do want your function to accept excess properties, you can modify the parameter type in order to allow excess properties explicitly by using an [index signature](https://www.typescriptlang.org/docs/handbook/2/objects.html#index-signatures):
187
-
188
- ```ts
189
- interface SomeProperties {
190
- foo: string;
191
-
192
- // This allows any other properties.
193
- // You may wish to make these types more specific according to your use case.
194
- [key: PropertKey]: unknown;
195
- }
196
-
197
- function serialize(x: SomeProperties): string {
198
- return JSON.stringify(x);
199
- }
200
-
201
- // No error!
202
- serialize({ foo: 'bar', anotherProperty: 'baz' });
203
- ```
204
-
205
- Which solution is appropriate is a case-by-case decision, depending on the intended use case of your function.
206
-
207
- ### I have a complex scenario that is reported by the rule, but I can't see how to remove the type parameter. What should I do?
208
-
209
- Sometimes, you may be able to rewrite the code by reaching for some niche TypeScript features, such as [the `NoInfer<T>` utility type](https://www.typescriptlang.org/docs/handbook/utility-types.html#noinfertype) (see [#9751](https://github.com/typescript-eslint/typescript-eslint/issues/9751)).
210
-
211
- But, quite possibly, you've hit an edge case where the type is being used in a subtle way that the rule doesn't account for.
212
- For example, the following arcane code is a way of testing whether two types are equal, and will be reported by the rule (see [#9709](https://github.com/typescript-eslint/typescript-eslint/issues/9709)):
213
-
214
- {/* prettier-ignore */}
215
- ```ts
216
- type Compute<A> = A extends Function ? A : { [K in keyof A]: Compute<A[K]> };
217
- type Equal<X, Y> =
218
- (<T1>() => T1 extends Compute<X> ? 1 : 2) extends
219
- (<T2>() => T2 extends Compute<Y> ? 1 : 2)
220
- ? true
221
- : false;
222
- ```
223
-
224
- In this case, the function types created within the `Equal` type are never expected to be assigned to; they're just created for the purpose of type system manipulations.
225
- This usage is not what the rule is intended to analyze.
226
-
227
- Use eslint-disable comments as appropriate to suppress the rule in these kinds of cases.
228
-
229
- {/* TODO - include an FAQ entry regarding instantiation expressions once the conversation in https://github.com/typescript-eslint/typescript-eslint/pull/9536#discussion_r1705850744 is done */}
230
-
231
- ## When Not To Use It
232
-
233
- This rule will report on functions that use type parameters solely to test types, for example:
234
-
235
- ```ts
236
- function assertType<T>(arg: T) {}
237
-
238
- assertType<number>(123);
239
- assertType<number>('abc');
240
- // ~~~~~
241
- // Argument of type 'string' is not assignable to parameter of type 'number'.
242
- ```
243
-
244
- If you're using this pattern then you'll want to disable this rule on files that test types.
245
-
246
- ## Further Reading
247
-
248
- - TypeScript handbook: [Type Parameters Should Appear Twice](https://microsoft.github.io/TypeScript-New-Handbook/everything/#type-parameters-should-appear-twice)
249
- - Effective TypeScript: [The Golden Rule of Generics](https://effectivetypescript.com/2020/08/12/generics-golden-rule/)
250
-
251
- ## Related To
252
-
253
- - eslint-plugin-etc's [`no-misused-generics`](https://github.com/cartant/eslint-plugin-etc/blob/main/docs/rules/no-misused-generics.md)
254
- - wotan's [`no-misused-generics`](https://github.com/fimbullinter/wotan/blob/master/packages/mimir/docs/no-misused-generics.md)
255
- - DefinitelyTyped-tools' [`no-unnecessary-generics`](https://github.com/microsoft/DefinitelyTyped-tools/blob/main/packages/eslint-plugin/docs/rules/no-unnecessary-generics.md)
@@ -1,98 +0,0 @@
1
- ---
2
- description: 'Disallow calling a function with a value with type `any`.'
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-unsafe-argument** for documentation.
11
-
12
- The `any` type in TypeScript is a dangerous "escape hatch" from the type system.
13
- Using `any` disables many type checking rules and is generally best used only as a last resort or when prototyping code.
14
-
15
- Despite your best intentions, the `any` type can sometimes leak into your codebase.
16
- Calling a function with an `any` typed argument creates a potential safety hole and source of bugs.
17
-
18
- This rule disallows calling a function with `any` in its arguments.
19
- That includes spreading arrays or tuples with `any` typed elements as function arguments.
20
-
21
- This rule also compares generic type argument types to ensure you don't pass an unsafe `any` in a generic position to a receiver that's expecting a specific type.
22
- For example, it will error if you pass `Set<any>` as an argument to a parameter declared as `Set<string>`.
23
-
24
- ## Examples
25
-
26
- <Tabs>
27
- <TabItem value="❌ Incorrect">
28
-
29
- ```ts
30
- declare function foo(arg1: string, arg2: number, arg3: string): void;
31
-
32
- const anyTyped = 1 as any;
33
-
34
- foo(...anyTyped);
35
- foo(anyTyped, 1, 'a');
36
-
37
- const anyArray: any[] = [];
38
- foo(...anyArray);
39
-
40
- const tuple1 = ['a', anyTyped, 'b'] as const;
41
- foo(...tuple1);
42
-
43
- const tuple2 = [1] as const;
44
- foo('a', ...tuple2, anyTyped);
45
-
46
- declare function bar(arg1: string, arg2: number, ...rest: string[]): void;
47
- const x = [1, 2] as [number, ...number[]];
48
- bar('a', ...x, anyTyped);
49
-
50
- declare function baz(arg1: Set<string>, arg2: Map<string, string>): void;
51
- baz(new Set<any>(), new Map<any, string>());
52
- ```
53
-
54
- </TabItem>
55
- <TabItem value="✅ Correct">
56
-
57
- ```ts
58
- declare function foo(arg1: string, arg2: number, arg3: string): void;
59
-
60
- foo('a', 1, 'b');
61
-
62
- const tuple1 = ['a', 1, 'b'] as const;
63
- foo(...tuple1);
64
-
65
- declare function bar(arg1: string, arg2: number, ...rest: string[]): void;
66
- const array: string[] = ['a'];
67
- bar('a', 1, ...array);
68
-
69
- declare function baz(arg1: Set<string>, arg2: Map<string, string>): void;
70
- baz(new Set<string>(), new Map<string, string>());
71
- ```
72
-
73
- </TabItem>
74
- </Tabs>
75
-
76
- There are cases where the rule allows passing an argument of `any` to `unknown`.
77
-
78
- Example of `any` to `unknown` assignment that are allowed:
79
-
80
- ```ts showPlaygroundButton
81
- declare function foo(arg1: unknown, arg2: Set<unknown>, arg3: unknown[]): void;
82
- foo(1 as any, new Set<any>(), [] as any[]);
83
- ```
84
-
85
- ## When Not To Use It
86
-
87
- If your codebase has many existing `any`s or areas of unsafe code, it may be difficult to enable this rule.
88
- It may be easier to skip the `no-unsafe-*` rules pending increasing type safety in unsafe areas of your project.
89
- 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.
90
-
91
- ## Related To
92
-
93
- - [Avoiding `any`s with Linting and TypeScript](/blog/avoiding-anys)
94
- - [`no-explicit-any`](./no-explicit-any.mdx)
95
- - [`no-unsafe-assignment`](./no-unsafe-assignment.mdx)
96
- - [`no-unsafe-call`](./no-unsafe-call.mdx)
97
- - [`no-unsafe-member-access`](./no-unsafe-member-access.mdx)
98
- - [`no-unsafe-return`](./no-unsafe-return.mdx)