Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

ESLint reports a let, const or function inside a switch case

ErrorUnexpected lexical declaration in case block.

Short answer

no-case-declarations fires when let, const, function or class is declared directly inside a case clause. The name is visible in the whole switch but is only set when that case runs. Wrap the case body in braces, or move the declaration out of the switch. Do not turn the rule off; it is part of the recommended set.

  • Applies to ESLint 9, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Run ESLint on the file and note which case the declaration sits in. The rule name in the output is no-case-declarations.

    npx eslint path/to/reducer.js
  2. 2Match what you see, then jump to its fix

    1. 1Unexpected lexical declaration in case block, pointing at a let or const line.let or const declared directly in a case clauseFix
    2. 2The same message, pointing at a function declaration or class in a case or default.A function or class is declared inside a case or default clauseFix

What does the error look like?

  • Unexpected lexical declaration in case block.

Cause 1 of 2

let or const declared directly in a case clause

1Check

The rule docs explain that the declaration is visible in the entire switch block but only gets initialised when the case that declares it is reached.

grep -n -A2 "case " path/to/reducer.js

2Fix

Wrap the clause in braces so each declaration has its own block.

switch (action.type) {
  case 'add': {
    const item = action.payload;
    return [...state, item];
  }
  case 'clear': {
    return [];
  }
}

3Verify

Run ESLint again; the report is gone.

npx eslint path/to/reducer.js

Confirmed by eslint.org

Did this fix it?

Cause 2 of 2

A function or class is declared inside a case or default clause

1Check

The rule's source treats function declarations, class declarations and let or const declarations as lexical; var is not flagged.

grep -n -B1 -A3 "default:" path/to/reducer.js

2Fix

Put the declaration inside a braced block in that clause, or move the helper above the switch so every case can use it.

function toLabel(x) { return String(x); }

switch (kind) {
  default: {
    return toLabel(value);
  }
}

3Verify

Run ESLint on the file.

npx eslint path/to/reducer.js

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Write every case body that declares a variable as a braced block from the start.
  • Keep switch cases short; move big case bodies into named functions.
  • Keep the rule on: the ESLint docs mark no-case-declarations as part of the recommended config.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

tsconfig rejects allowImportingTsExtensions

An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly

allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.