Skip to content
← Back to rules

typescript/no-unnecessary-type-arguments 懸念

💭 This rule requires type information.
🛠️ An auto-fix is available for this rule.

何を行うか

このルールは、デフォルトの型パラメータと同一である型引数の使用を禁止します。

なぜ問題か

デフォルト値と同じである明示的な型引数は不要であり、コードに視覚的なノイズを加えます。TypeScript はこれらの型を自動的に推論します。

このルールにおいて不正なコードの例:

ts
function identity<T = string>(arg: T): T {
  return arg;
}

// 無駄な型引数 - string がデフォルト値
const result = identity<string>("hello");

interface Container<T = number> {
  value: T;
}

// 無駄な型引数 - number がデフォルト値
const container: Container<number> = { value: 42 };

class MyClass<T = boolean> {
  constructor(public value: T) {}
}

// 無駄な型引数 - boolean がデフォルト値
const instance = new MyClass<boolean>(true);

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

ts
function identity<T = string>(arg: T): T {
  return arg;
}

// デフォルト型を使用
const result1 = identity("hello");

// 異なる型を使用
const result2 = identity<number>(42);

interface Container<T = number> {
  value: T;
}

// デフォルト型を使用
const container1: Container = { value: 42 };

// 異なる型を使用
const container2: Container<string> = { value: "hello" };

使用方法

設定ファイルまたは CLI でこのルールを有効化するには、以下のいずれかを使用できます:

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

参照