no-unnecessary-type-arguments
Disallow type arguments that are equal to the default.
Extending "plugin:@typescript-eslint/strict-type-checked"
in an ESLint configuration enables this rule.
Some problems reported by this rule are automatically fixable by the --fix
ESLint command line option.
This rule requires type information to run.
Type parameters in TypeScript may specify a default value. For example:
function f<T = 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.
module.exports = {
"rules": {
"@typescript-eslint/no-unnecessary-type-arguments": "error"
}
};
Try this rule in the playground ↗
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 PlaygroundOptions
This rule is not configurable.
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 degredations after enabling type checked rules.