question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

VS Code displays an error on type annotation

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
maximeduprecommented, May 20, 2020

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.

0reactions
benwoodwardcommented, Feb 17, 2022

Not sure if this is related but I’m getting the same error on a number annotation using Neovim:

image

tsconfig:

{
  "extends": "@tsconfig/svelte/tsconfig.json",
  "compilerOptions": {
    "moduleResolution": "node",
    "module": "esnext",
    "lib": ["esnext"],
    "target": "es2019",
    "strict": true,
    "noImplicitAny": false,
    /**
      svelte-preprocess cannot figure out whether you have a value or a type, so tell TypeScript
      to enforce using \`import type\` instead of \`import\` for Types.
      */
    "importsNotUsedAsValues": "error",
    "allowSyntheticDefaultImports": true,
    "isolatedModules": true,
    "resolveJsonModule": true,
    /**
      To have warnings/errors of the Svelte compiler at the correct position,
      enable source maps by default.
      */
    "sourceMap": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "baseUrl": ".",
    "allowJs": true,
    "checkJs": true,
    "paths": {
      "$lib": ["src/lib"],
      "$lib/*": ["src/lib/*"]
    }
  },
  "include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.ts", "src/**/*.svelte"],
  "exclude": ["node_modules/*"]
}

EDIT: Nevermind, I was missing lang="ts" on the script tag. Should have been <script lang="ts">

Read more comments on GitHub >

github_iconTop Results From Across the Web

Syntax highlighting not working for Python in VS Code with ...
I'm trying to get Visual Studio Code to format (the colours not the layout) Python code with type annotations (hinting).
Read more >
TypeScript editing with Visual Studio Code
VS Code's TypeScript IntelliSense understands many standard JSDoc annotations, and uses them to show typing information and documentation in suggestions, hover ...
Read more >
TypeScript Programming with Visual Studio Code
You'll notice the TypeScript keyword let and the string type declaration. ... Ctrl+Shift+M to display the PROBLEMS panel with a list of all...
Read more >
Working with JavaScript in Visual Studio Code
VS Code allows you to leverage some of TypeScript's advanced type checking and error reporting functionality in regular JavaScript files. This is a...
Read more >
Syntax Highlight Guide | Visual Studio Code Extension API
Syntax highlighting determines the color and style of source code displayed in the Visual Studio Code editor. It is responsible for colorizing keywords...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found