no-unnecessary-type-constraint
Disallow unnecessary constraints on generic types.
Attributes
- Included in configs
- ✅ Recommended
- 🔒 Strict
- Fixable
- 🔧 Automated Fixer
- 🛠 Suggestion Fixer
- 💭 Requires type information
Rule Details
Type parameters (<T>
) may be "constrained" with an extends
keyword (docs).
When not provided, type parameters happen to default to:
It is therefore redundant to extend
from these types in later versions of TypeScript.
Examples of code for this rule:
- ❌ Incorrect
- ✅ Correct
interface FooAny<T extends any> {}
interface FooUnknown<T extends unknown> {}
type BarAny<T extends any> = {};
type BarUnknown<T extends unknown> = {};
class BazAny<T extends any> {
quxUnknown<U extends unknown>() {}
}
class BazUnknown<T extends unknown> {
quxUnknown<U extends unknown>() {}
}
const QuuxAny = <T extends any>() => {};
const QuuxUnknown = <T extends unknown>() => {};
function QuuzAny<T extends any>() {}
function QuuzUnknown<T extends unknown>() {}
interface Foo<T> {}
type Bar<T> = {};
class Baz<T> {
qux<U> { }
}
const Quux = <T>() => {};
function Quuz<T>() {}
Options
// .eslintrc.json
{
"rules": {
"@typescript-eslint/no-unnecessary-type-constraint": "error"
}
}
This rule is not configurable.
When Not To Use It
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.