no-unnecessary-type-arguments
Disallow type arguments that are equal to the default.
在 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.
- Flat Config
- Legacy Config
export default tseslint.config({
rules: {
"@typescript-eslint/no-unnecessary-type-arguments": "error"
}
});
module.exports = {
"rules": {
"@typescript-eslint/no-unnecessary-type-arguments": "error"
}
};
在线运行试试这个规则 ↗
示例
¥Examples
- ❌ Incorrect
- ✅ Correct
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选项
该规则不可配置。
何时不使用它
¥When Not To Use It
如果你更喜欢显式指定类型参数,即使它们等于默认值,也可以跳过此规则。
¥If you prefer explicitly specifying type parameters even when they are equal to the default, you can skip this rule.
Type checked lint rules are more powerful than traditional lint rules, but also require configuring type checked linting.
See Troubleshooting > Linting with Type Information > Performance if you experience performance degradations after enabling type checked rules.
'## 资源'