UNPKG

1.87 kBMarkdownView Raw
1# `no-unnecessary-type-constraint`
2
3Disallows unnecessary constraints on generic types.
4
5## Rule Details
6
7Type parameters (`<T>`) may be "constrained" with an `extends` keyword ([docs](https://www.typescriptlang.org/docs/handbook/generics.html#generic-constraints)).
8When 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
13It is therefore redundant to `extend` from these types in later versions of TypeScript.
14
15Examples of code for this rule:
16
17<!--tabs-->
18
19### ❌ Incorrect
20
21```ts
22interface FooAny<T extends any> {}
23interface FooUnknown<T extends unknown> {}
24
25type BarAny<T extends any> = {};
26type BarUnknown<T extends unknown> = {};
27
28class BazAny<T extends any> {
29 quxUnknown<U extends unknown>() {}
30}
31
32class BazUnknown<T extends unknown> {
33 quxUnknown<U extends unknown>() {}
34}
35
36const QuuxAny = <T extends any>() => {};
37const QuuxUnknown = <T extends unknown>() => {};
38
39function QuuzAny<T extends any>() {}
40function QuuzUnknown<T extends unknown>() {}
41```
42
43### ✅ Correct
44
45```ts
46interface Foo<T> {}
47
48type Bar<T> = {};
49
50class Baz<T> {
51 qux<U> { }
52}
53
54const Quux = <T>() => {};
55
56function 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
70This rule is not configurable.
71
72## When Not To Use It
73
74If 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