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>() {}

How to use

To enable this rule in the CLI or using the config file, you can use:

bash
oxlint --deny typescript/no-unnecessary-type-constraint
json
{
  "rules": {
    "typescript/no-unnecessary-type-constraint": "error"
  }
}

References

Released under the MIT License.