217

I am starting to use TypeScript in a Node project I am working on in Visual Studio Code. I wanted to follow the "opt-in" strategy, similar to Flow. Therefore I put // @ts-check at the top of my .js file in hope to enable TS for that file. Ultimately I want the same experience of "linting" as Flow, therefore I installed the plugin TSLint so I could see Intellisense warnings/errors.

But with my file looking like:

// @ts-check

module.exports = {
  someMethod: (param: string): string => {
    return param;
  },
};

and my tsconfig.json file looking like...

{
  "compilerOptions": {
      "target": "es2016",
      "module": "commonjs",
      "allowJs": true
  }
}

I get this error: [js] 'types' can only be used in a .ts file. as shown below in the image.

error from vscode for ts

I saw this question which recommended disabling javascript validation in vscode but then that doesn't show me any TypeScript Intellisense info.

I tried setting tslint.jsEnable to true in my vscode settings as mentioned in the TSLint extension docs but no luck there.

What is the correct setup in order to use .js files with TypeScript and get Intellisense so I know what the errors in my code are before I run any TS commands?

isherwood
  • 58,414
  • 16
  • 114
  • 157
james
  • 5,006
  • 8
  • 39
  • 64
  • Do you have any error if you change the extension to ts? – Israel Zinc Feb 19 '18 at 03:34
  • 1
    @israel.zinc changing the extension to `.ts` show me the TS errors/warnings as expected. I guess this could work, but I was hoping for more of the opt-in method using `@ts-check` while keeping all my extensions as `.js` – james Feb 19 '18 at 03:49
  • Possible duplicate of [Visual Studio Code:\[js\] types can only be used in a .ts file](https://stackoverflow.com/questions/41320266/visual-studio-codejs-types-can-only-be-used-in-a-ts-file) – BuZZ-dEE Jun 29 '19 at 23:36
  • 1
    @BuZZ-dEE it might be, but the answers there did not solve the issue. I mentioned this in my original question. – james Jul 02 '19 at 14:17
  • Please consider changing the accepted answer to the correct one: https://stackoverflow.com/a/50726464/2333214 – T J Dec 03 '21 at 10:57

15 Answers15

345

I'm using flow with vscode but had the same problem. I solved it with these steps:

  1. Install the extension Flow Language Support

  2. Disable the built-in TypeScript extension:

    1. Go to Extensions tab
    2. Search for @builtin TypeScript and JavaScript Language Features
    3. Click on Disable
KyleMit
  • 30,350
  • 66
  • 462
  • 664
Idan Dagan
  • 10,273
  • 5
  • 34
  • 41
  • 8
    The accepted answer is not always correct. There's a bug in VSCode with React Native and Flow that gives the same message in .js files, and in that case YOU SHOULD NOT change to .ts, but continue with .js and fix the problem as suggested here!! – pashute May 17 '18 at 13:24
  • 275
    Just an FYI: Disabling this extension basically nullifies all of the nice things about JS development with VSCode. Instead, you should just add the following into your settings json file: `"javascript.validate.enable": false` – heez May 22 '18 at 21:52
  • 6
    as @heez has mentioned this will affect, finding reference to javascript files, autocompletion etc don't disable Javascript Language Features. – Ajitsen Dec 29 '18 at 12:35
  • This disables things like going to function definitions. – GEMI Jan 16 '19 at 20:13
  • 4
    The valid answer was said by @heez – asimolmez Jul 20 '19 at 14:41
  • Thanks @heez! when using flow, also `"javascript.validate.enable": false` to get rid of other typescript related validation errors that don't apply – DerpyNerd Mar 13 '20 at 15:34
  • @heez what is the file name of settings json you are talking about? – Anish Arya Apr 04 '20 at 11:38
  • @AnishArya I believe it's called `settings.json`. You can read more about its default location depending on your platform here https://code.visualstudio.com/docs/getstarted/settings – heez Apr 06 '20 at 14:13
  • 1
    @heez thanks for the save, I found out. No error now. Can use Visual studio code finally. – Anish Arya Apr 06 '20 at 14:20
  • I also had to install that specific version of flow-bin given in .flowconfig file to make that work. Otherwise I just got several errors in my node_modules directory. – martintrapp Nov 27 '20 at 09:11
  • 1
    making "javascript.validate.enable": false is not an actual solution but it just hides it, it just doesnt check the file/code. even if you write random/invalid code, vscode will not run into an error. does anyone found the CORRECT resolution? – Aventus Jun 25 '21 at 05:36
239

Use "javascript.validate.enable": false in your VS Code settings, It doesn't disable ESLINT. I use both ESLINT & Flow. Simply follow the instructions Flow For Vs Code Setup

Adding this line in settings.json. Helps "javascript.validate.enable": false

chengsam
  • 7,315
  • 6
  • 30
  • 38
Adeel Imran
  • 13,166
  • 8
  • 62
  • 77
  • 11
    With this You don't have to disable typescript. Both can exists. Best solution! – NiRUS Oct 18 '18 at 05:48
  • 5
    If you are using ESLint to validate your Javascript, then this answer is the best solution. More info: https://code.visualstudio.com/docs/languages/javascript#_how-do-i-disable-syntax-validation-when-using-non-es6-constructs and also https://github.com/flowtype/flow-for-vscode#setup – Beau Smith Dec 06 '18 at 18:59
  • 3
    Where can I find settings.json file.? – i18n Feb 10 '20 at 12:04
  • 2
    Hi @i18n Go to: Code -> Preferences -> Settings -> Extensions -> Scroll down and find "Edit in settings.json". Actually find the one under this: `Include Languages Enable Emmet abbreviations in languages that are not supported by default. Add a mapping here between the language and emmet supported language. E.g.: {"vue-html": "html", "javascript": "javascriptreact"}` click on Edit in settings.json and add this: `"javascript.validate.enable": false`. – Fotios Tsakiris Mar 24 '20 at 14:46
  • 5
    Good solution, note that you can also do this per project/workspace. So you can have normal `typescript` validation in one, and `flow` validation in the other. look for the `Workspace` tab on the settings page. – tsi Jan 20 '21 at 07:53
  • 1
    To find this in settings (UI), search "javascript validate". It's located under Extensions > Typescript. – tim-phillips Feb 25 '21 at 01:49
  • This is not working for my multi-root workspace. When I try to disable that for only one folder, it says: `This setting cannot be applied in this workspace. It will be applied when you open the containing workspace folder directly.` ... is it possible to setup with multi-root WS? – Philipp Kyeck Sep 27 '21 at 11:52
  • 1
    Added a separate question for this ☝️ https://stackoverflow.com/questions/69346413/setup-flowjs-in-a-vscode-multi-root-workspace-for-only-one-folder – Philipp Kyeck Sep 27 '21 at 12:15
  • 1
    Before this configuration my `TypeScript and JavaScript Language Features` startup activation was more than 2500ms now I got 58ms. Thank you It was very helpfull – Bladimir Medrano Vargas Oct 23 '21 at 16:35
38

I followed following steps to fix the issue:

Go to Preferences and then to settings: Settings

After going to settings search for javascript.validate in the search bar and uncheck the checkbox: text in search bar

if you want to edit it via json then: Go to: Code -> Preferences -> Settings -> Extensions -> Scroll down and find "Edit in settings.json". extension then add

"javascript.validate.enable": false

showing settings.json

surhidamatya
  • 2,419
  • 32
  • 56
6

For anyone who lands here and all the other solutions did not work give this a try. I am using typescript + react and my problem was that I was associating the files in vscode as javascriptreact not typescriptreact so check your settings for the following entries.

   "files.associations": {
    "*.tsx": "typescriptreact",
    "*.ts": "typescriptreact"
  },
Andy
  • 523
  • 2
  • 13
Kenny Hammerlund
  • 436
  • 4
  • 14
3

The solution that worked for me was adding the following property to settings.json:

"js/ts.implicitProjectConfig.checkJs": true

To enable type checking for all JavaScript files without changing any code, just add "js/ts.implicitProjectConfig.checkJs": true to your workspace or user settings. This enables type checking for any JavaScript file that is not part of a jsconfig.json or tsconfig.json project.

- https://code.visualstudio.com/docs/nodejs/working-with-javascript#_type-checking-javascript

pgarciacamou
  • 1,602
  • 22
  • 40
3

Go to settings and search for javascript validate and then disable the option to fix this.

settings

Fcmam5
  • 4,888
  • 1
  • 16
  • 33
JayK
  • 76
  • 6
2

You must use a .ts file - e.g. test.ts to get Typescript validation, intellisense typing of vars, return types, as well as "typed" error checking (e.g. passing a string to a method that expects an number param will error out).

It will be transpiled into (standard) .js via tsc.


Update (11/2018):

Clarification needed based on down-votes, very helpful comments and other answers.

types

  • Yes, you can do type checking in VS Code in .js files with @ts-check - as shown in the animation

  • What I originally was referring to for Typescript types is something like this in .ts which isn't quite the same thing:

    hello-world.ts

    function hello(str: string): string {
      return 1;
    }
    
    function foo(str:string):void{
       console.log(str);
    }
    

    This will not compile. Error: Type "1" is not assignable to String

  • if you tried this syntax in a Javascript hello-world.js file:

    //@ts-check
    
    function hello(str: string): string {
      return 1;
    }
    
    function foo(str:string):void{
       console.log(str);
    }
    

    The error message referenced by OP is shown: [js] 'types' can only be used in a .ts file

If there's something I missed that covers this as well as the OP's context, please add. Let's all learn.

EdSF
  • 11,753
  • 6
  • 42
  • 83
  • I have found a few articles that suggest you can use all the features of TS within a JS file. Here's one from [Smashing Magazine](https://www.smashingmagazine.com/2018/01/visual-studio-code/#type-check-your-javascript), a [GitHub issue for vscode](https://github.com/Microsoft/vscode/issues/25322) that describes the setup, and some [release notes for vscode](https://code.visualstudio.com/updates/v1_12#_type-checking-for-javascript-files) – james Feb 19 '18 at 03:53
  • @jamez14 I haven't tried/had the need to _not_ use `.ts`. IINM, the samples show _error checking_ and _intellisense_. The issue you have is (static) `type`ing your variables, and return `types` as well - e.g. `foo:string`, `bar:number`, `fubar:MyClass`, `myMethod(f:Array):void`, `anotherMethod():string`. Hth... – EdSF Feb 19 '18 at 05:31
  • yep, looks like you are right with your assessment. After looking more at the examples, I'm seeing it is just JS error checking, not TS, when using the `@ts-check` attribute. Very misleading... – james Feb 19 '18 at 13:39
  • Upvoting this because I was dumb in svelte and did not type in the .ts and this generically useful answer helped. – Dirk Bester Feb 16 '23 at 03:53
  • See this https://www.typescriptlang.org/tsconfig#checkJs to avoid putting that line in all your js files – perrocallcenter Jun 16 '23 at 04:35
2

[Mac] In your Visual Studio Code type the following command.

  • command + shift + P
  • Then type " Preferences: Open User Settings (JSON)" in the search bar shown on the top of the VS Code
  • Add the the line -> "javascript.validate.enable": false

Note : "Preferences: Open Default Settings (JSON)" is non editable

Gayathri
  • 21
  • 3
1

For me, the issue was not initializing my component with lang="ts" e.g. <script setup lang="ts">

Dan
  • 147
  • 2
  • 8
0

"typescript.validate.enable": false

IN VS Code -> Preferences -> Settings -> Search for above mentioned property and disable the property

0

Types/Interfaces can only be applied to TypeScript files, and .js files are JavaScript files.

So the solution is simple as to:

Change the extension .js to .tsx to use types/interfaces.

Leandro Ariel
  • 727
  • 8
  • 5
0

Just disable the built-in TypeScript extension

follow steps as below

SpiderMan
  • 15
  • 5
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/34434497) – blackcityhenry May 24 '23 at 07:44
-1

For VS code in windows, click on settings. Then on the search bar at the top, type javascript validate and just uncheck the checkbox as shown below. enter image description here

kayleb
  • 19
  • 3
-1

If you are using .svelte, try putting lang="ts" in your <script> element. Example as below.

<script lang="ts">
   ...
</script>
Gerald H
  • 454
  • 4
  • 13
-2

Just default the variable to the expected type:

(number=1) => ...
(number=1.0) => ...
(string='str') ...
dippas
  • 58,591
  • 15
  • 114
  • 126