# @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`.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. This rule needs types, so turning it on makes `bun lint` type check your code first.

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

Every option, set to its default:

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

{/* End of the generated section. */}

## Examples

Reported:

{/* bad */}
```ts
function greet({ name = "guest" }: { name: string }) {
  return `Hello, ${name}`;
}
```

{/* bad */}
```ts
const doubled = [1, 2, 3].map((value = 0) => value * 2);
```

{/* bad */}
```ts
const point: [number, number] = [1, 2];
const [x = 0] = point;
```

{/* bad {"count": 2} */}
```ts
function connect(port = undefined) {}

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

Not reported:

{/* good */}
```ts
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:

{/* good */}
```ts
function connect(port: number = 3000) {}

connect();
```

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

{/* bad */}
```ts
function connect(port: number | undefined = undefined) {}
```

{/* good */}
```ts
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:

{/* bad {"count": 1, "compilerOptions": {"strict": false}} */}
```ts
const port = 3000;
```
