no-useless-default-assignment
Disallow default values that will never be used.
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, which comes with performance tradeoffs.
Default parameters and default values are only used if the parameter or property is undefined.
That can happen when a value is missing, or when one is provided and set to undefined.
If a non-undefined value is guaranteed to be provided, then there is no need to define a default.
- Flat Config
- Legacy Config
export default tseslint.config({
rules: {
"@typescript-eslint/no-useless-default-assignment": "error"
}
});
module.exports = {
"rules": {
"@typescript-eslint/no-useless-default-assignment": "error"
}
};
Try this rule in the playground ↗
Examples
- ❌ Incorrect
- ✅ Correct
function Bar({ foo = '' }: { foo: string }) {
return foo;
}
const { foo = '' } = { foo: 'bar' };
const [foo = ''] = ['bar'];
[1, 2, 3].map((a = 42) => a + 1);
Open in Playgroundfunction Bar({ foo = '' }: { foo?: string }) {
return foo;
}
const { foo = '' } = { foo: undefined };
const [foo = ''] = [undefined];
[1, 2, 3, undefined].map((a = 42) => a + 1);
Open in PlaygroundOptions
This rule is not configurable.
When Not To Use It
If you use default values defensively against runtime values that bypass type checking, or for documentation purposes, you may want to disable 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.