@typescript-eslint/no-useless-default-assignment

Disallow default values that can never be used

has suggestionsneeds types

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.

bunfig.toml
[lint.rules]
"@typescript-eslint/no-useless-default-assignment" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"@typescript-eslint/no-useless-default-assignment" = ["error", {
  allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing = false,
}]

Examples#

example.ts1 problem
function greet({ name = "guest" }: { name: string }) {
  return `Hello, ${name}`;
}
example.ts1 problem
const doubled = [1, 2, 3].map((value = 0) => value * 2);
example.ts1 problem
const point: [number, number] = [1, 2];
const [x = 0] = point;
example.ts2 problems
function connect(port = undefined) {}

const { host = undefined } = { host: "localhost" };
example.tsNo problems
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:

example.tsNo problems
function connect(port: number = 3000) {}

connect();

Use ? to make a parameter optional, not = undefined:

example.ts1 problem
function connect(port: number | undefined = undefined) {}
example.tsNo problems
function connect(port?: number) {}

Options#

OptionDefaultWhat it does
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoingfalseWithout 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:

example.ts1 problem
const port = 3000;