Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seeking Help: How to Resolve "Cannot redeclare block-scoped variable" Across Different Virtual Files #241

Open
FliPPeDround opened this issue Oct 24, 2024 · 2 comments

Comments

@FliPPeDround
Copy link

While editing HTML files in VSCode, I've noticed that redeclaring the same variable within <script type="module"> tags still triggers an error "Cannot redeclare block-scoped variable 'xxx'", despite each module supposedly being independent.
image
To address this, I attempted using Volar.js by placing them in different virtual files, yet I still encountered the error "file://home/xxx/starter/sample/test.html(2, 5): 'bar' was also declared here.", yet this also triggers recognition and does not solve my problem.
image

I have had difficulty finding more documentation on Volar and am unsure if it can handle this situation. If you have any suggestions or solutions, I would greatly appreciate it. Also, please let me know if Volar.js does not yet support this functionality. Thank you for your time and assistance.

@johnsoncodehk
Copy link
Member

In order for TS to think that the virtual script is a module, you need to place empty exports in each virtual script.

For example, for the following script tag:

<script type"module">
const a = 1;
console.log(a);
</script>

Its virtual code should be:

const a = 1;
console.log(a);

export { };

@FliPPeDround
Copy link
Author

FliPPeDround commented Oct 25, 2024

Thank you very much for your help! Adding export {} solved the error issue with the virtual file module. However, even though the virtual file no longer shows errors, the original file's language server validations seem unchanged. Is it possible for Volar to take over these validation rules to prevent this code from showing errors? Is such a feature feasible?
image
Additionally, I tried adding the following configuration in VSCode to address the issue:

{
  "html.validate.scripts": false
}

However, I still encounter type inference errors.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants