Language-tools: VS Code displays an error on type annotation

Created on 20 May 2020  ·  5Comments  ·  Source: sveltejs/language-tools

I have the Svelte Beta extension installed and have restarted the language server. My svelte.config.js is:

const sveltePreprocess = require('svelte-preprocess');

module.exports = {
    preprocess: sveltePreprocess(),
};

However, the extension does not seem to help VS Code understand that my code is Typescript, since it gives an error on type annotation:

Type annotations can only be used in TypeScript files. javascript

Screen Shot 2020-05-19 at 8 24 24 PM

Also interesting, if I change lang="typescript" to type="text/typescript", the syntax colours completely go away, as well as the error:

Screen Shot 2020-05-19 at 8 26 22 PM

System (please complete the following information):

  • OS: macOS Catalina 10.15.4
  • IDE: VS Code
  • Plugin: Svelte Beta
bug

Most helpful comment

Ah, so the problem was because I had this in my VS Code "User" settings:

  "files.associations": {
    "*.svelte": "html"
  },

Now that I've removed it, everything seems to work fine.

All 5 comments

Can you post the entire file's code here or an example repo? I can't reproduce it. Don't need to include all the script but needs other top-level tags

Here's a minimal reproduction:

<script lang="typescript">
    let countExcludingUser: number = 0;
</script>

<p>Hi</p>

Screen Shot 2020-05-19 at 8 53 22 PM

I disabled all extensions except Svelte Beta and restarted VS Code, with no success.

I only managed to reproduce once and no luck after that. Can you try it again it the latest build 99.0.22? There is some update about preprocessing in this version. If it still persists can you make an example repo?

From your description it seems that SveltePlugin.ts does get that it should preprocess with Typescript because there is no Svelte syntax error like "unexpected token". On the other hand TypescriptPlugin.ts does - for some reason - not recognize that you use Typescript but thinks it's Javascript instead. Really strange.. An example repo would certainly help.

Ah, so the problem was because I had this in my VS Code "User" settings:

  "files.associations": {
    "*.svelte": "html"
  },

Now that I've removed it, everything seems to work fine.

Was this page helpful?
0 / 5 - 0 ratings