12
votes

Is it possible to use .js files in a typescript project in vs code? I have clone a react-native project from a github repository and opened it in visual studio code. When I add a tsconfig.json in order to start using typescript, I get a long list of errors. For example here is a drawer.js file (with es6 features):

enter image description here

Here is my tsconfig.json file (If I remove this file then every thing works fine, no error reported) :

enter image description here

4
Why do you need the '.js' extension, instead of '.ts'?martinarroyo
As I said I cloned this project from github. It contains tens of .js files code base (es6 syntax). I opened it in vs code and added a tsconfig.json. Typescript is supposed to be a superset of javascript. So I would expect that a .js file should not be a problem in a typescript project.TheSoul
Have a look at these threads 1 and 2. There seems to be an --allowJS option that does exactly that: https://github.com/Microsoft/TypeScript/wiki/What%27s-new-in-TypeScript#including-js-files-with---allowjs. The trick part however is for Visual Code to accept the files... I am not sure if it uses the values of tsconfig.json for syntax checking.martinarroyo
Unfortunately adding allowJS has not solved the problem. I have added "allowJS" in my tsconfig.json file. I'm still having the errorTheSoul

4 Answers

7
votes

I restarted VS code and the error was gone, just in case it helps

7
votes

NOTE - This is just a WORKAROUND, not solution. Use ONLY IF you do not find appropriate solution

Workaround - Add the following configuration in $workspace/.vscode/settings.json

{
    "javascript.validate.enable": false
}
1
votes

You can try with this!!!

Install in your visual studio code the extension Flow Language Support and disable TypeScript and JavaScript Language Features

Search @builtin TypeScript and JavaScript Language Features and reload.

You can see this solutions https://stackguides.com/questions/48859169

Its important install npm for flow

Link Flow

1
votes

Has that change the "Select Language Mode" for "TypeScript React" in list. it worked for me.

enter image description here