Skip to content
← Back to rules

typescript/no-unnecessary-type-constraint Suspicious

🚧 An auto-fix is planned for this rule, but not implemented at this time.

何をするか

ジェネリック型パラメータに対する不要な制約を禁止します。

なぜ問題なのか

TypeScript のジェネリック型パラメータ(<T>)は、extends キーワードで「制約」を設定できます。extends が指定されない場合、型パラメータのデフォルトの制約は unknown になります。したがって、anyunknown から継承する必要はありません。

このルールに対して誤りなコードの例:

typescript
interface FooAny<T extends any> {}
interface FooUnknown<T extends unknown> {}

type BarAny<T extends any> = {};
type BarUnknown<T extends unknown> = {};

const QuuxAny = <T extends any>() => {};

function QuuzAny<T extends any>() {}
typescript
class BazAny<T extends any> {
  quxAny<U extends any>() {}
}

このルールに対して正しいコードの例:

typescript
interface Foo<T> {}

type Bar<T> = {};

const Quux = <T>() => {};

function Quuz<T>() {}
typescript
class Baz<T> {
  qux<U>() {}
}

使い方

このルールを設定ファイルまたは CLI で有効化するには、次のように使用できます:

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

参照