-
-
Notifications
You must be signed in to change notification settings - Fork 53
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
Comments
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 { }; |
Enable Volar Takeover ModeEnsure that Volar is managing TypeScript instead of VS Code's default. Add the following to your {
"volar.takeover.mode.enabled": true,
"typescript.tsserver.experimental.enableProjectDiagnostics": true
}
#### Prevent VS Code's default validation for <script> blocks by adding this configuration:
{
"html.validate.scripts": false
}
Use TypeScript Module Type:
Make sure your <script> tag includes the type="module" attribute:
|
Add the following configurations to your settings.json:
This allows Volar to fully manage TypeScript, ensuring compatibility with <script lang="ts"> blocks.
this to your settings.json:
This prevents duplicate or conflicting validations from VS Code's default HTML language features. Example:
HTML File :>
###Troubleshooting
|
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.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.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.The text was updated successfully, but these errors were encountered: