13

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

TheSoul
  • 4,906
  • 13
  • 44
  • 74
  • Why do you need the '.js' extension, instead of '.ts'? – martinarroyo Dec 25 '16 at 14:11
  • 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 Dec 25 '16 at 14:15
  • Have a look at these threads [1](https://github.com/Microsoft/TypeScript/issues/2302) and [2](https://github.com/Microsoft/TypeScript/issues/10939). 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 Dec 25 '16 at 14:20
  • Unfortunately adding allowJS has not solved the problem. I have added "allowJS" in my tsconfig.json file. I'm still having the error – TheSoul Jan 11 '17 at 12:17

4 Answers4

9

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

RoRFan
  • 344
  • 4
  • 8
7

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
}
Swapnil Patwa
  • 4,069
  • 3
  • 25
  • 37
  • 13
    This is not a solution. – kos Feb 22 '18 at 00:31
  • 3
    This simply disables validation completely, which removes the error, but does not solve the larger problem of checking types in JS files. – Matt Hudson Apr 22 '18 at 19:54
  • It's honestly a bug with react and this is still a valid workaround if you don't want all those "errors" – Relativity Dec 04 '19 at 12:17
  • Actually, this is the *only* solution I got to work when using a Flow project with VSCode. In that case, Flow takes over the validation. See also [this answer](https://stackoverflow.com/a/50385231/286685) and specifically the comments on that answer. – Stijn de Witt Aug 02 '22 at 07:54
1

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://stackoverflow.com/questions/48859169

Its important install npm for flow

Link Flow

1

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

enter image description here

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129