Error: 'types' can only be used in a .ts file - Visual Studio Code using @ts-check

JavascriptTypescriptVisual Studio-Code

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

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

Solution 2 - Javascript

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

Solution 3 - Javascript

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

Solution 4 - Javascript

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

Solution 5 - Javascript

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.

Solution 6 - Javascript

"typescript.validate.enable": false

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

Solution 7 - Javascript

Just default the variable to the expected type:

(number=1) => ...
(number=1.0) => ...
(string='str') ...

Solution 8 - Javascript

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjamesView Question on Stackoverflow
Solution 1 - JavascriptIdan DaganView Answer on Stackoverflow
Solution 2 - JavascriptAdeel ImranView Answer on Stackoverflow
Solution 3 - JavascriptsurhidamatyaView Answer on Stackoverflow
Solution 4 - JavascriptKenny HammerlundView Answer on Stackoverflow
Solution 5 - JavascriptEdSFView Answer on Stackoverflow
Solution 6 - JavascriptSrini KarthikeyanView Answer on Stackoverflow
Solution 7 - Javascriptuser14812174View Answer on Stackoverflow
Solution 8 - JavascriptkaylebView Answer on Stackoverflow