Skip to content

noUselessDefaultAssignment (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"noUselessDefaultAssignment": "error"
}
}
}
}

Disallow default values that can never be used.

A default value only applies when the value it replaces is undefined. If the type says the value can’t be undefined, the default is dead code.

This rule checks destructured object properties, destructured tuple elements, and parameters of callbacks passed directly to a call.

invalid-parameter-property.ts
function greet({ name = "friend" }: { name: string }) {
return `Hello, ${name}`;
}
/invalid-parameter-property.ts:1:25 lint/nursery/noUselessDefaultAssignment  FIXABLE  ━━━━━━━━━━━━━━

ℹ This default value is never used.

> 1 │ function greet({ name = "friend" }: { name: string }) {
│ ^^^^^^^^
2 │ return `Hello, ${name}`;
3 │ }

ℹ A default value only applies when the value is undefined, but the type of this property doesn't include undefined.

ℹ Remove the default value, or add undefined to the type if the value can be missing.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Remove the default value.

1 │ function·greet({·name·=·"friend"·}:·{·name:·string·})·{
│ -----------
invalid-variable-property.ts
const { name = "friend" } = { name: "Ada" };
/invalid-variable-property.ts:1:16 lint/nursery/noUselessDefaultAssignment  FIXABLE  ━━━━━━━━━━━━━━━

ℹ This default value is never used.

> 1 │ const { name = "friend" } = { name: "Ada" };
│ ^^^^^^^^
2 │

ℹ A default value only applies when the value is undefined, but the type of this property doesn't include undefined.

ℹ Remove the default value, or add undefined to the type if the value can be missing.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Remove the default value.

1 │ const·{·name·=·"friend"·}·=·{·name:·"Ada"·};
│ -----------
invalid-tuple-element.ts
declare const pair: [string, number];
const [label = "none", count] = pair;
/invalid-tuple-element.ts:2:16 lint/nursery/noUselessDefaultAssignment  FIXABLE  ━━━━━━━━━━━━━━━━━━━

ℹ This default value is never used.

1 │ declare const pair: [string, number];
> 2 │ const [label = "none", count] = pair;
│ ^^^^^^
3 │

ℹ A default value only applies when the value is undefined, but the type of this array element doesn't include undefined.

ℹ Remove the default value, or add undefined to the type if the value can be missing.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Remove the default value.

2 │ const·[label·=·"none",·count]·=·pair;
│ ---------
invalid-callback-parameter.ts
declare function onMessage(listener: (message: string) => void): void;
onMessage((message = "") => console.log(message));
/invalid-callback-parameter.ts:2:22 lint/nursery/noUselessDefaultAssignment  FIXABLE  ━━━━━━━━━━━━━━

ℹ This default value is never used.

1 │ declare function onMessage(listener: (message: string) => void): void;
> 2 │ onMessage((message = "") => console.log(message));
│ ^^
3 │

ℹ A default value only applies when the value is undefined, but the type of this parameter doesn't include undefined.

ℹ Remove the default value, or add undefined to the type if the value can be missing.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Remove the default value.

2 │ onMessage((message·=·"")·=>·console.log(message));
│ -----
function greet({ name = "friend" }: { name?: string }) {
return `Hello, ${name}`;
}
const { label = "none" } = { label: undefined };
declare const names: string[];
const [first = "nobody"] = names;
declare function onMessage(listener: (message?: string) => void): void;
onMessage((message = "") => console.log(message));