3

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.

Screenshot of Error

How can I make that error go away in VS Code?

5 Answers 5

5

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your response, I know that .ts get full IDE supports. But in js file I get full typescript support to with this configuration, the problem is vscode validate type annotation and expect it only for .ts files
Did you try the JS doc annotation . Doesnt that work for you?
3

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"
}

1 Comment

This helps but VSCode restart is required, as specified by @jony89 answer and according to my experience
2

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.

Comments

0

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.

1 Comment

Thanks for your response, I know that .ts get full IDE supports. But in js file I get full typescript support to with this configuration, the problem is vscode validate type annotation and expect it only for .ts files
0

If you also want VS Code to highlight TypeScript (declared is JSDoc comments) errors in JS files, this setting helps:

{
    "js/ts.implicitProjectConfig.checkJs": true
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.