1 | # `no-unnecessary-type-constraint`
|
2 |
|
3 | Disallows unnecessary constraints on generic types.
|
4 |
|
5 | ## Rule Details
|
6 |
|
7 | Type parameters (`<T>`) may be "constrained" with an `extends` keyword ([docs](https://www.typescriptlang.org/docs/handbook/generics.html#generic-constraints)).
|
8 | When not provided, type parameters happen to default to:
|
9 |
|
10 | - As of TypeScript 3.9: `unknown` ([docs](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-9.html#type-parameters-that-extend-any-no-longer-act-as-any))
|
11 | - Before that, as of 3.5: `any` ([docs](https://devblogs.microsoft.com/typescript/announcing-typescript-3-5/#breaking-changes))
|
12 |
|
13 | It is therefore redundant to `extend` from these types in later versions of TypeScript.
|
14 |
|
15 | Examples of code for this rule:
|
16 |
|
17 |
|
18 |
|
19 | ### ❌ Incorrect
|
20 |
|
21 | ```ts
|
22 | interface FooAny<T extends any> {}
|
23 | interface FooUnknown<T extends unknown> {}
|
24 |
|
25 | type BarAny<T extends any> = {};
|
26 | type BarUnknown<T extends unknown> = {};
|
27 |
|
28 | class BazAny<T extends any> {
|
29 | quxUnknown<U extends unknown>() {}
|
30 | }
|
31 |
|
32 | class BazUnknown<T extends unknown> {
|
33 | quxUnknown<U extends unknown>() {}
|
34 | }
|
35 |
|
36 | const QuuxAny = <T extends any>() => {};
|
37 | const QuuxUnknown = <T extends unknown>() => {};
|
38 |
|
39 | function QuuzAny<T extends any>() {}
|
40 | function QuuzUnknown<T extends unknown>() {}
|
41 | ```
|
42 |
|
43 | ### ✅ Correct
|
44 |
|
45 | ```ts
|
46 | interface Foo<T> {}
|
47 |
|
48 | type Bar<T> = {};
|
49 |
|
50 | class Baz<T> {
|
51 | qux<U> { }
|
52 | }
|
53 |
|
54 | const Quux = <T>() => {};
|
55 |
|
56 | function Quuz<T>() {}
|
57 | ```
|
58 |
|
59 | ## Options
|
60 |
|
61 | ```jsonc
|
62 | // .eslintrc.json
|
63 | {
|
64 | "rules": {
|
65 | "@typescript-eslint/no-unnecessary-type-constraint": "error"
|
66 | }
|
67 | }
|
68 | ```
|
69 |
|
70 | This rule is not configurable.
|
71 |
|
72 | ## When Not To Use It
|
73 |
|
74 | 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.
|
75 |
|
76 | ## Attributes
|
77 |
|
78 | - Configs:
|
79 | - [x] ✅ Recommended
|
80 | - [x] 🔒 Strict
|
81 | - [ ] 🔧 Fixable
|
82 | - [ ] 💭 Requires type information
|