Skip to content
← Back to rules

typescript/no-unnecessary-type-parameters Suspicious

💭 This rule requires type information.

它的作用

禁止声明了但并未被有意义地使用的类型参数。

为什么这不好?

不必要的类型参数会让签名更冗长、更难理解,而且它们 往往会掩盖简化 API 的机会。

示例

以下是此规则的错误代码示例:

ts
function parseYAML<T>(input: string): T {
  return input as any as T;
}

以下是此规则的正确代码示例:

ts
function parseYAML(input: string): unknown {
  return input;
}

function identity<T>(value: T): T {
  return value;
}

如何使用

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

json
{
  "options": {
    "typeAware": true
  },
  "rules": {
    "typescript/no-unnecessary-type-parameters": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  options: { typeAware: true },
  rules: {
    "typescript/no-unnecessary-type-parameters": "error",
  },
});
bash
oxlint --type-aware --deny typescript/no-unnecessary-type-parameters

版本

此规则已在 v1.49.0 中添加。

参考资料