@typescript-eslint/no-useless-default-assignment
Disallow default values that can never be used
Also called typescript-eslint/no-useless-default-assignment, typescript/no-useless-default-assignment
A default value only applies when the value is undefined. If the type says that a property, a tuple element, or a callback parameter is always there, the default never runs, and it tells readers that the value can be missing when it can't.
A default of undefined is reported too, because a missing value is already undefined.
Configure#
Off by default. This rule needs types, so turning it on makes bun lint type check your code first.
[lint.rules]
"@typescript-eslint/no-useless-default-assignment" = "error" # or "warn"Every option, set to its default:
[lint.rules]
"@typescript-eslint/no-useless-default-assignment" = ["error", {
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing = false,
}]Examples#
function greet({ name = "guest"Default value is useless because the property is not optional. }: { name: string }) {
return `Hello, ${name}`;
}const doubled = [1, 2, 3].map((value = 0Default value is useless because the parameter is not optional.) => value * 2);const point: [number, number] = [1, 2];
const [x = 0Default value is useless because the property is not optional.] = point;function connect(port = undefinedDefault value is useless because it is undefined. Optional parameters are already undefined by default.) {}
const { host = undefinedDefault value is useless because it is undefined. Optional propertys are already undefined by default. } = { host: "localhost" };function greet({ name = "guest" }: { name?: string }) {
return `Hello, ${name}`;
}
const doubled = [1, 2, 3].map(value => value * 2);In a function declaration, the default is what makes the parameter optional, so it's allowed:
function connect(port: number = 3000) {}
connect();Use ? to make a parameter optional, not = undefined:
function connect(port: number | undefined = undefinedUsing = undefined to make a parameter optional adds unnecessary runtime logic.) {}function connect(port?: number) {}Options#
| Option | Default | What it does |
|---|---|---|
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing | false | Without strictNullChecks in tsconfig.json, the rule can't tell what can be undefined, and it reports that once for each file. Set this to true to turn that report off. |
Without strictNullChecks in tsconfig.json, types don't say what can be undefined. By default, the rule reports each of those files once, to tell you to turn it on:
cThis rule requires the strictNullChecks compiler option to be turned on to function correctly.onst port = 3000;