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

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

问题描述

我开始在我在 Visual Studio Code 中处理的 Node 项目中使用 TypeScript.我想遵循类似于 Flow 的选择加入"策略.因此,我将 //@ts-check 放在我的 .js 文件的顶部,希望为该文件启用 TS.最终我想要与 Flow 相同的linting"体验,因此我安装了插件

我看到了这个问题 建议在 vscode 中禁用 javascript 验证,但这并没有向我显示 any TypeScript Intellisense 信息.

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

为了在 TypeScript 中使用 .js 文件并获得 Intellisense 以便我在运行任何 TS 命令之前知道代码中的错误是什么,正确的设置是什么?

解决方案

我正在使用 flow 和 vscode但有同样的问题.我通过以下步骤解决了它:

  1. 安装扩展Flow语言支持

  2. 禁用内置的 TypeScript 扩展:

    1. 转到扩展标签
    2. 搜索@builtin TypeScript 和 JavaScript 语言功能
    3. 点击禁用

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.

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?

解决方案

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

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

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