AphrodyBun GitHub

Runtime docs · Linter · Rules

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

Disallow default values that can never be used

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

Reported:

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

const { host = undefined } = { host: "localhost" };

Not reported:

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 = undefined) {}
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:

const port = 3000;