Skip to content
Latchkey

tsc TS2451: Cannot redeclare block-scoped variable 'x' in CI

TS2451 means a block-scoped name (let/const) is declared more than once in the same scope. In CI it most often comes from a file with no imports/exports being treated as a global script that collides with another.

What this error means

tsc fails with "error TS2451: Cannot redeclare block-scoped variable 'config'." sometimes reported twice, once per declaration site across two files.

tsc
src/a.ts:1:7 - error TS2451: Cannot redeclare block-scoped variable 'config'.

1 const config = loadA();
        ~~~~~~

src/b.ts:1:7 - error TS2451: Cannot redeclare block-scoped variable 'config'.

Common causes

Two script files share the global scope

A file with no top-level import/export is a global script, so its top-level const collides with the same name in another script file.

A genuine duplicate declaration in one scope

The same let/const name is declared twice within a single block or module.

How to fix it

Make the files modules

  1. Add an import or export so each file becomes a module with its own scope.
  2. An empty export {} is enough to convert a script to a module.
  3. Re-run tsc to confirm the names no longer collide.
src/a.ts
// a.ts
export {}; // makes this a module, not a global script
const config = loadA();

Remove a real duplicate declaration

When both declarations are in one scope, rename or delete one so the name is declared once.

How to prevent it

  • Keep files as modules (with imports/exports) so top-level names are scoped.
  • Add export {} to any otherwise import-free file you intend as a module.
  • Avoid reusing the same top-level const name across global scripts.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →