Skip to main content

no-unnecessary-initialization

Disallow unnecessary initialization to undefined.

Targeted pattern scope

This rule checks for explicit undefined initializers in two places:

  • variable declarators (const value = undefined;), and
  • class property definitions (field = undefined;).

Only direct identifier undefined is matched.

What this rule reports

This rule reports variables and class fields explicitly initialized with undefined.

Why this rule exists

Initializing to undefined is usually redundant in JavaScript/TypeScript. Removing these initializers makes intent clearer and avoids unnecessary syntax.

❌ Incorrect

const value = undefined;
class C {
field = undefined;
}

✅ Correct

let value: number | undefined;
class C {
field?: number;
}

Behavior and migration notes

This rule reports only and does not provide an autofix.

In most cases, migration is a direct deletion of = undefined.

Options

This rule has no options.

Additional examples

let cache = undefined;
// ❌ reported

let cache: string | undefined;
// ✅ valid

ESLint flat config example

import etcMisc from "eslint-plugin-etc-misc";

export default [
{
plugins: { "etc-misc": etcMisc },
rules: {
"etc-misc/no-unnecessary-initialization": "error",
},
},
];

When not to use it

Disable this rule if your project prefers explicit undefined initializers for clarity.

Package documentation

Rule catalog ID: R047

Further reading

Adoption resources

  • Start at warning level in CI, then move to error after cleanup.
  • Use focused codemods/autofix batches per package or directory.