js'types'只能在.ts文件中使用-使用@ ts-check的Visual Studio Code [英] js 'types' can only be used in a .ts file - Visual Studio Code using @ts-check
问题描述
我开始在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:
-
安装扩展流语言支持
禁用内置的TypeScript扩展名:
Disable the built-in TypeScript extension:
- 转到扩展程序标签
- 搜索 @内置的TypeScript和JavaScript语言功能
- 点击禁用
- Go to Extensions tab
- Search for @builtin TypeScript and JavaScript Language Features
- Click on Disable
这篇关于js'types'只能在.ts文件中使用-使用@ ts-check的Visual Studio Code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!