Language-tools: Bug: Using the auto-subscription syntax for a store that was defined in the context="module" script yields an error.

Created on 2 Mar 2021  ·  6Comments  ·  Source: sveltejs/language-tools

Take a look at the Test component in the following REPL:
https://svelte.dev/repl/4859affa8c1c436783a9d9a6d537c903?version=3

A store is created in a context="module" script tag, and it's being used with a dollar sign $ prepended to it, in the markup. It works as expected. However, the very same code in VS Code shows an error:

image

Note that it works and compiles properly, in other words, the compiler gives no errors; so it must be the Svelte VS Code extension that's the problem, basically it doesn't properly recognize any store that's been defined in the context="module" script, and as a result causing a red squiggly to appear under $count here.

Also, it's only when the lang="ts" attribute is present on the <script> tag that the red squiggly appears.

I'd appreciate it if this can be fixed soon.

Fixed bug

Most helpful comment

Using the auto-subscription syntax for a store that was defined in the context="module" script should no longer yield an error as of VS Code extension 104.6.0 / svelte-check 1.2.1

All 6 comments

  1. Faced the same problem.
    Error: Cannot find name '$username'. Did you mean 'username'? (ts) when username created in context="module".

image

  1. There is one more error when running svelte-check:
> svelte-check --ignore src/node_modules/@sapper

/src/routes/my/test.svelte:2:2
Hint: '$username' is declared but its value is never read. (ts)
<script lang="ts">
  import { username } from './_store';

Code:
image

Version:

    "svelte": "^3.35.0",
    "svelte-check": "^1.2.0",
    "typescript": "^4.2.2"

@fivemru Is the screenshot correct though? Because as it appears you're not defining username in the a context="module" script tag but in a separate file.

@fivemru Is the screenshot correct though? Because as it appears you're not defining username in the a context="module" script tag but in a separate file.

The screenshot is for the 2nd case. Added one more for the 1st.
In the 2nd case, the error does not appear in vscode, but it is there when running svelte-check

@fivemru Oh okay. Got it.

The second error is unrelated. One could argue it's correct because you only write to the store and never read it. By doing $username = .. you tell Svelte to subscribe to that store but you never use it. In this case username.set(..) is the better option. Still I get that this is confusing so we may look into removing that hint.

Using the auto-subscription syntax for a store that was defined in the context="module" script should no longer yield an error as of VS Code extension 104.6.0 / svelte-check 1.2.1

Was this page helpful?
0 / 5 - 0 ratings