Reputation: 965
Generally I want to write .js
files with typescript instead of Flow. I configured the webpack to use ts-loader
on js extension, and that works just fine. I use checkJs
on tsconfig file and it check the js file fine.
However, VS Code shows an error the error on js files:
Type annotations can only be used in TypeScript files.
How can I make that error go away in VS Code?
Upvotes: 3
Views: 2880
Reputation: 5367
Since Typescript 3 is out, and the work they have done allowing it to be combined with babel 7, it makes sense now for many programmers to have typescript in .js
files.
Currently I have added the following vscode settings :
"files.associations": {
"*.js": "typescript"
},
And restarted VSCode, and it works for me.
Hopefully vscode will provide a cleaner solution in the future.
Upvotes: 2
Reputation: 65613
Yes you generally should use the correct file extension but you can force VS Code to treat JS files as TypeScript by setting:
"files.associations": {
"*.js": "typescript"
}
Upvotes: 3
Reputation: 1964
You cannot use typescript type declarations in js files. (Even with checkJS enabled)
On JS files you have to use JSDoc annotations.
/** @type {number} */
var x;
Typescript would check these for you.
But I guess what you're looking for is a .ts file
Upvotes: 5
Reputation: 1288
You should use .ts, not .js files for TypeScript code in order to get full IDE and other tooling support. The compiler will transform your .ts files into .js.
Upvotes: 0