js'types'只能在.ts文件中使用-使用@ ts-check的Visual Studio Code [英] js 'types' can only be used in a .ts file - Visual Studio Code using @ts-check

查看:291
本文介绍了js'types'只能在.ts文件中使用-使用@ ts-check的Visual Studio Code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始在Visual Studio Code中正在使用的Node项目中使用TypeScript.我想遵循类似于Flow的选择加入"策略.因此,我将// @ts-check放在我的.js文件的顶部,希望为该文件启用TS.最终,我希望获得与Flow一样的棉绒"体验,因此我安装了插件 TSLint 这样我就可以看到Intellisense警告/错误.

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.

但是我的文件如下:

// @ts-check

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

和我的tsconfig.json文件看起来像...

and my tsconfig.json file looking like...

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

我收到此错误:[js] 'types' can only be used in a .ts file.,如下图所示.

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

我看到了这个问题建议在vscode中禁用javascript验证,但随后不会显示任何 TypeScript Intellisense信息.

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

我尝试将我的vscode设置中的tslint.jsEnable设置为true,如TSLint扩展文档中所述,但是没有运气.

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

什么是正确的设置,以便将.js文件与TypeScript一起使用并获取Intellisense,这样我在运行任何TS命令之前就知道代码中的错误是什么?

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?

推荐答案

我正在将 flow 与vscode结合使用但是有同样的问题.我通过以下步骤解决了这个问题:

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

  1. 安装扩展流语言支持

禁用内置的TypeScript扩展名:

Disable the built-in TypeScript extension:

  1. 转到扩展程序标签
  2. 搜索 @内置的TypeScript和JavaScript语言功能
  3. 点击禁用
  1. Go to Extensions tab
  2. Search for @builtin TypeScript and JavaScript Language Features
  3. Click on Disable

这篇关于js'types'只能在.ts文件中使用-使用@ ts-check的Visual Studio Code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆