DEV Community

Cover image for TypeScript and the Error: Cannot Redeclare Block Scoped Variable <Name>
Luiz Calaça
Luiz Calaça

Posted on • Edited on

3 2

TypeScript and the Error: Cannot Redeclare Block Scoped Variable <Name>

Hi, Devs!

Who are getting start in Typescript could face this error: "Cannot Redeclare Block Scoped Variable Name" and it means a concept into that language.

Let's see an example:

Cannot Redeclare Block Scoped Variable Name

The error is coming because we have a TypeScript feature.

1 - The file needs to be declared as a module (exports) with its own scope.
2 - The script into file will be comprehended in a global scope.

The first example is the 2 option. So, how can we solve that? Let's see another example with the export:

export and create a scope module

Therefore, when we use the export it's created a module with its own scope and not shared in global scope.

Is there other possibility? Yes! Use namespace

namespace Greetings {
    function returnGreeting (greeting: string) {
        console.log(`The message from namespace Greetings is ${greeting}.`);
    }
}

Enter fullscreen mode Exit fullscreen mode

All the things into namespace it's out of global scope. You can put functions or classes into there.

Contacts
Email: luizcalaca@gmail.com
Instagram: https://www.instagram.com/luizcalaca
Linkedin: https://www.linkedin.com/in/luizcalaca/
Twitter: https://twitter.com/luizcalaca

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (1)

Collapse
 
shriekdj profile image
Shrikant Dhayje

Thanks dude this solved my issue

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay