Skip to content

typescript/no-unnecessary-type-constraint Suspicious

What it does

Disallow unnecessary constraints on generic types.

Why is this bad?

Generic type parameters (<T>) in TypeScript may be "constrained" with an extends keyword. When no extends is provided, type parameters default a constraint to unknown. It is therefore redundant to extend from any or unknown.

Example

typescript
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> {
  quxAny<U extends any>() {}
}
const QuuxAny = <T extends any>() => {};
function QuuzAny<T extends any>() {}

References

Released under the MIT License.