A code editor showing a duplicate let declaration with a red error underline, illustrating the cannot redeclare block-scoped variable error
Back to Blog
Frontend Development
December 30, 2019
Soumya Rauth

Cannot redeclare block-scoped variable. The Reason behind the error and the way to resolve it.

Why the Cannot redeclare block-scoped variable error shows up in JavaScript and TypeScript, and the cleanest way to resolve it.

A code editor showing a duplicate let declaration with a red error underline, illustrating the cannot redeclare block-scoped variable error

Few days back I was trying to write a script in my Visual Studio Code IDE, where I was trying to declare and initialize a variable with the keyword let.

But, I was faced with an error. Part of the error message was, Cannot redeclare block-scoped variable.

VS Code showing the TypeScript error Cannot redeclare block-scoped variable var1, ts 2451, pointing at a second declaration in main.js

So, I dug a bit into it to find out the reason behind the error. What I found was:

  1. Because, the let keyword is block scoped and unlike var, it can not be re-declared or reassigned within the same block.
  2. After Transpiling, the var1 variable of my main.ts file is also now available on the Transpiled main.js file as "var var1".
  3. Right now after the Transpilation there are essentially two variables with the same name var1; one of which is in the main.ts which is declared as let (remember let is not redeclared or reassigned) and another one is in main.js
  4. So naturally visual studio code displays the error on main.ts file because the variable in there, is declared with the let keyword.
  5. As a result it won't allow to Transpile the file and displays Cannot redeclare block-scoped variable "variable name".

How to resolve?

Typescript is moduler and each module has it's own block. So, basically if you could somehow enclose the variable which is declared as let within it's own module, the error will be resolved because now your let variable is in it's separate  block.

To achieve this, simply type export on the top of your script or additionally you can type export {}. Now you will see that the error is resolved.

The same main.ts file with export {} added above the let declaration, which clears the error

Hope this article helps you finding out and resolving the issue. Thanks a bunch! for reading out.

Need this inside a real product? ARITS does web app and platform engineering.

Need extra engineers, or a team to run the build?

Tell us the shape of the work and your deadline. You will get a straight read on what it takes and what it will cost, from a team that has shipped 400+ projects out of Dhaka and London.

Cookies

We use cookies to see how people find this site and to measure our ads. You can turn those off — everything here still works. Privacy policy

Necessary

Keeps the site working and remembers this choice.

Always on

Analytics

Anonymous stats on which pages get read, so we know what to write next.

Marketing

Lets us measure which ads brought someone here.