typescript/no-unnecessary-type-arguments Suspicious
作用
此规则不允许与默认类型参数相同的类型实参。
为什么这不好?
显式指定与默认值相同的类型实参是没有必要的,并且会给代码增加视觉噪音。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" };如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"options": {
"typeAware": true
},
"rules": {
"typescript/no-unnecessary-type-arguments": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
options: { typeAware: true },
rules: {
"typescript/no-unnecessary-type-arguments": "error",
},
});bash
oxlint --type-aware --deny typescript/no-unnecessary-type-arguments版本
此规则于 v1.12.0 中添加。
