Skip to main content

no-shadow

Disallow shadowing variables from outer scopes.

Targeted pattern scopeโ€‹

This rule walks lexical scopes and reports variable declarations that reuse names from parent scopes.

Enum declarations are intentionally ignored.

What this rule reportsโ€‹

This rule reports variables that reuse names from outer scopes.

Enum declarations are skipped both when evaluating the current declaration and when evaluating potential outer-scope matches.

Why this rule existsโ€‹

Shadowing can hide outer bindings and make data flow harder to reason about.

โŒ Incorrectโ€‹

const x = 1;
function f() {
const x = 2;
return x;
}

โœ… Correctโ€‹

const x = 1;
function f() {
const y = x + 1;
return y;
}

Deprecatedโ€‹

Behavior and migration notesโ€‹

This rule is deprecated in favor of @typescript-eslint/no-shadow.

It reports only and does not provide an autofix.

Optionsโ€‹

This rule has no options.

Statusโ€‹

Use the Deprecated section above for lifecycle details.

Additional examplesโ€‹

enum Status {
Ready,
}

function run(Status: number): number {
return Status;
}
// enum-related declarations are intentionally ignored by this rule.

ESLint flat config exampleโ€‹

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

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

When not to use itโ€‹

Disable this rule if shadowed variable names are acceptable in your code style.

Package documentationโ€‹

Rule catalog ID: R040

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.