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.