VS Code "Type annotations can only be used in TypeScript files"
mvolkmann opened this issue · 12 comments
Do you use VS Code? If so, have you found a way to stop it from complaining about type annotations in .svelte
files?
I thought what I needed to do was the following, but this didn't work.
-
npm install -D svelte-preprocess
-
Create a
svelte.config.js
file at the root of the project containing this:
const sveltePreprocess = require('svelte-preprocess');
module.exports = {
preprocess: sveltePreprocess()
};
- Configure it in
rollup.config.js
like this:
...
import sveltePreprocess from 'svelte-preprocess';
...
export default {
...
plugins: [
svelte({
...
preprocess: sveltePreprocess()
}),
...
],
...
};
...
- Restart VS Code
But after all this VS Code still complains about type annotations in .svelte
files.
Maybe the answer is that I need to use the VS Code "Svelte Type Checker" extension. I tried that, but I still get the same errors on all my type annotations. See halfnelson/svelte-type-checker-vscode#5.
Does this still happen?
Yes. I just walked through all the steps again with a new Svelte project. VS Code puts a red squiggly below every type annotation. Hovering over them gives a popup that says "Type annotations can only be used in TypeScript files."
When viewing a .svelte
file the language in the status bar is "HTML". I disabled all extensions except Svelte Beta and Svelte Type Checker. Then I reloaded the window. Nothing changed. I still get "Type annotations can only be used in TypeScript files."
You are correct that if I remove this, the warnings about type annotations go away.
"files.associations": {
"*.svelte": "html"
}
But then I get new problems like the inability to run Prettier on .svelte
files on save. But this isn't your issue. It seems I have to pick my poison and I'd rather have format on save working. The perfect VS Code setup for Svelte still eludes me.
The svelte plugin has never worked for me and it hasn't been updated since October 7, 2019. That's why I went with suggestions to mark .svelte files as HTML.