no-unnecessary-type-constraint
Disallow unnecessary constraints on generic types.
✅
Extending "plugin:@typescript-eslint/recommended"
in an ESLint configuration enables this rule.
💡
Some problems reported by this rule are manually fixable by editor suggestions.
Generic type parameters (<T>
) in TypeScript may be "constrained" with an extends
keyword.
When no extends
is provided, type parameters default a constraint to any
.
It is therefore redundant to extend
from any
.
.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/no-unnecessary-type-constraint": "error"
}
};
Examples
- ❌ Incorrect
- ✅ Correct
interface FooAny<T extends any> {}
type BarAny<T extends any> = {};
class BazAny<T extends any> {
quxAny<U extends any>() {}
}
const QuuxAny = <T extends any>() => {};
function QuuzAny<T extends any>() {}
interface Foo<T> {}
type Bar<T> = {};
class Baz<T> {
qux<U> { }
}
const Quux = <T>() => {};
function Quuz<T>() {}
This rule is not configurable.
Options
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.