Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 369
james Avatar asked Feb 19 '18 03:02

james


People also ask

Can we use JS in TS file?

The allowJs setting allows JavaScript files to be imported inside your TypeScript files. The setting basically allows JavaScript and TypeScript files to live in the same project.

How do I enable a TS check?

The easiest way to enable type checking in a JavaScript file is by adding // @ts-check to the top of a file.

What is TS in Visual Studio code?

TypeScript in Visual Studio Code. TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. It offers classes, modules, and interfaces to help you build robust components.


1 Answers

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
like image 112
Idan Dagan Avatar answered Nov 03 '22 01:11

Idan Dagan