|
| 1 | +--- |
| 2 | +description: 'Disallow type parameters that only appear once.' |
| 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 only appear once in a function, method, or class definition. |
| 13 | + |
| 14 | +Type parameters relate two types. |
| 15 | +If a type parameter only appears 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 Early Stage |
| 22 | +This rule was recently added to typescript-eslint and still considered experimental. |
| 23 | +It might change significantly between minor versions. |
| 24 | +Please try it out and give us feedback! |
| 25 | +::: |
| 26 | + |
| 27 | +## Examples |
| 28 | + |
| 29 | +<Tabs> |
| 30 | +<TabItem value="❌ Incorrect"> |
| 31 | + |
| 32 | +```ts |
| 33 | +function second<A, B>(a: A, b: B): B { |
| 34 | + return b; |
| 35 | +} |
| 36 | + |
| 37 | +function parseJSON<T>(input: string): T { |
| 38 | + return JSON.parse(input); |
| 39 | +} |
| 40 | + |
| 41 | +function printProperty<T, K extends keyof T>(obj: T, key: K) { |
| 42 | + console.log(obj[key]); |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +</TabItem> |
| 47 | +<TabItem value="✅ Correct"> |
| 48 | + |
| 49 | +```ts |
| 50 | +function second<B>(a: unknown, b: B): B { |
| 51 | + return b; |
| 52 | +} |
| 53 | + |
| 54 | +function parseJSON(input: string): unknown { |
| 55 | + return JSON.parse(input); |
| 56 | +} |
| 57 | + |
| 58 | +function printProperty<T>(obj: T, key: keyof T) { |
| 59 | + console.log(obj[key]); |
| 60 | +} |
| 61 | + |
| 62 | +// T appears twice: in the type of arg and as the return type |
| 63 | +function identity<T>(arg: T): T { |
| 64 | + return arg; |
| 65 | +} |
| 66 | + |
| 67 | +// T appears twice: "keyof T" and in the inferred return type (T[K]). |
| 68 | +// K appears twice: "key: K" and in the inferred return type (T[K]). |
| 69 | +function getProperty<T, K extends keyof T>(obj: T, key: K) { |
| 70 | + return obj[key]; |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +</TabItem> |
| 75 | +</Tabs> |
| 76 | + |
| 77 | +## Limitations |
| 78 | + |
| 79 | +Note that this rule allows any type parameter that is used multiple times, even if those uses are via a type argument. |
| 80 | +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: |
| 81 | + |
| 82 | +```ts |
| 83 | +declare function createStateHistory<T>(): T[]; |
| 84 | +``` |
| 85 | + |
| 86 | +This is because the type parameter `T` relates multiple methods in the `T[]` together, making it used more than once. |
| 87 | + |
| 88 | +Therefore, this rule won't report on type parameters used as a type argument. |
| 89 | +That includes type arguments given to global types such as `Array` (including the `T[]` shorthand and in tuples), `Map`, and `Set`. |
| 90 | + |
| 91 | +## When Not To Use It |
| 92 | + |
| 93 | +This rule will report on functions that use type parameters solely to test types, for example: |
| 94 | + |
| 95 | +```ts |
| 96 | +function assertType<T>(arg: T) {} |
| 97 | + |
| 98 | +assertType<number>(123); |
| 99 | +assertType<number>('abc'); |
| 100 | +// ~~~~~ |
| 101 | +// Argument of type 'string' is not assignable to parameter of type 'number'. |
| 102 | +``` |
| 103 | + |
| 104 | +If you're using this pattern then you'll want to disable this rule on files that test types. |
| 105 | + |
| 106 | +## Further Reading |
| 107 | + |
| 108 | +- TypeScript handbook: [Type Parameters Should Appear Twice](https://microsoft.github.io/TypeScript-New-Handbook/everything/#type-parameters-should-appear-twice) |
| 109 | +- Effective TypeScript: [The Golden Rule of Generics](https://effectivetypescript.com/2020/08/12/generics-golden-rule/) |
| 110 | + |
| 111 | +## Related To |
| 112 | + |
| 113 | +- eslint-plugin-etc's [`no-misused-generics`](https://github.com/cartant/eslint-plugin-etc/blob/main/docs/rules/no-misused-generics.md) |
| 114 | +- wotan's [`no-misused-generics`](https://github.com/fimbullinter/wotan/blob/master/packages/mimir/docs/no-misused-generics.md) |
| 115 | +- DefinitelyTyped-tools' [`no-unnecessary-generics`](https://github.com/microsoft/DefinitelyTyped-tools/blob/main/packages/eslint-plugin/docs/rules/no-unnecessary-generics.md) |
0 commit comments