no-unnecessary-type-arguments
不允许与默认值相等的类型参数.
🔒
在 ESLint 配置 中扩展"plugin:@typescript-eslint/strict-type-checked"
可启用此规则。
🔧
此规则报告的一些问题可通过 --fix
ESLint 命令行选项自动修复。
💭
该规则需要 类型信息 才能运行,但这会带来性能方面的权衡。
TypeScript 中的类型参数可以指定默认值。例如:
¥Type parameters in TypeScript may specify a default value. For example:
function f<T = number>(/* ... */) {
// ...
}
提供等于默认值的显式类型参数是多余的:例如调用 f<number>(...)
。此规则报告何时显式指定的类型参数是该类型参数的默认值。
¥It is redundant to provide an explicit type parameter equal to that default: e.g. calling f<number>(...)
.
This rule reports when an explicitly specified type argument is the default for that type parameter.
- 扁平配置
- 旧版配置
eslint.config.mjs
export default tseslint.config({
rules: {
"@typescript-eslint/no-unnecessary-type-arguments": "error"
}
});
.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/no-unnecessary-type-arguments": "error"
}
};
在线运行试试这个规则 ↗
示例
¥Examples
- ❌ 错误
- ✅ 正确
function f<T = number>() {}
f<number>();
Open in Playgroundfunction g<T = number, U = string>() {}
g<string, string>();
Open in Playgroundclass C<T = number> {}
new C<number>();
class D extends C<number> {}
Open in Playgroundinterface I<T = number> {}
class Impl implements I<number> {}
Open in Playgroundfunction f<T = number>() {}
f();
f<string>();
Open in Playgroundfunction g<T = number, U = string>() {}
g<string>();
g<number, number>();
Open in Playgroundclass C<T = number> {}
new C();
new C<string>();
class D extends C {}
class D extends C<string> {}
Open in Playgroundinterface I<T = number> {}
class Impl implements I<string> {}
Open in Playground选项
该规则不可配置。