"Cannot redeclare block-scoped variable" validation error

Have you ever encountered the following validation error while coding in Velo?

Cannot redeclare block-scoped variable x.

Take a look at the below video which shows it in a more visualized way (for a const named ‘schema’):


Why does this happen?

  1. Velo currently uses a TypeScript compiler for autocomplete and code validations. Yes, Velo users TypeScript typing system to validate your Velo JS code :slight_smile:

  2. Read the explanation in this post, it will clarify things.

If typescript recognizes a code file as a script (code that doesn’t include import or export statements) it shares global scope with all other script files. In Velo, there’s currently one global scope for everything (including frontend and backend - same scope). So Basically this could happen in two scenarios.
One is that you are trying to declare a variable which you declared already in another “script" file
Another scenario is that you declare a variable that collides with one of the properties of the global scope object. “name” is such an example when writing frontend code in Velo because it’s already been declared by the WebWorker (our frontend runtime environment)


What can you do about it?

  1. Change your variable name (clever, hu?)

  2. Export or import something in one of the files in which you declare the variable, as it would make that file a module which has its own scope.

  3. Another option is to move that variable declaration to a function or the onReady event handler if that fits your need.

Hope you’d find this helpful in case you’ve encountered this validation error.

Got something to add? Please share it with us in the comments section below.

4 Likes