typescript 如何禁用:[js] 文件是一个 CommonJS 模块;它可以转换为 ES6 模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49582984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:19:27  来源:igfitidea点击:

How do I disable: [js] File is a CommonJS module; it may be converted to an ES6 module

typescriptvisual-studio-code

提问by SunburntRock89

The following is what I want to disable:

以下是我要禁用的内容:

[js] File is a CommonJS module; it may be converted to an ES6 module.

[js] File 是一个 CommonJS 模块; 它可以转换为 ES6 模块。

I can't find it in settings.
Help appreciated as this is really annoying.

我在设置里找不到
帮助表示赞赏,因为这真的很烦人。

回答by HSir

This is a new feature added in Visual Studio Code called "Suggestion Code Actions". "Suggestion Code Actions" are enabled by default in JavaScript and TypeScript.

这是 Visual Studio Code 中添加的一项新功能,称为“建议代码操作”。“建议代码操作”在 JavaScript 和 TypeScript 中默认启用。

You can disable them by setting: "typescript.suggestionActions.enabled": falseor "javascript.suggestionActions.enabled": falsein your user/workspace settings. The documentation can be found here.

您可以通过设置:"typescript.suggestionActions.enabled": false"javascript.suggestionActions.enabled": false在您的用户/工作区设置中禁用它们。文档可以在这里找到。

(Image provided by Yusuf Ya?ar.)

(图片由Yusuf Ya?ar 提供。)

回答by weakish

Actually this annoying suggestion comes from TypeScript.

实际上,这个烦人的建议来自 TypeScript。

Thus, to turn off this suggestion, you can modify the source code of TypeScript, compile it, then tell vscode to use your fork of TypeScript.

因此,要关闭此建议,您可以修改 TypeScript 的源代码,编译它,然后告诉 vscode 使用您的 TypeScript 分支。

As a quick and dirty hack, just remove the logic related to ts.Diagnostics.File_is_a_CommonJS_module_it_may_be_converted_to_an_ES6_module, then compile the project following the instructions on TypeScript's README.

作为一个快速而肮脏的黑客,只需删除与 相关的逻辑ts.Diagnostics.File_is_a_CommonJS_module_it_may_be_converted_to_an_ES6_module,然后按照 TypeScript 的 README 上的说明编译项目。

The compilation will fail because removing the related logic causes some functions become unused, then you just remove those unused function definitions and recompile the project (gulp clean && gulp local).

编译会失败,因为删除相关逻辑会导致一些函数变得不使用,然后您只需删除那些未使用的函数定义并重新编译项目(gulp clean && gulp local)。

After you successfully compile your fork of TypeScript, then change your user settings.json to point to your vscode fork:

在您成功编译 TypeScript 的 fork 之后,然后更改您的用户 settings.json 以指向您的 vscode fork:

"typescript.tsdk": "/path/to/your/fork/of/TypeScript/built/local",

Done.

完毕。

Restart your vscode, and the annoying suggestion has gone.

重新启动你的 vscode,烦人的建议就消失了。

You can check this committo see which source files of TypeScript need to be modify.

您可以检查此提交以查看需要修改哪些 TypeScript 源文件。

Warn: the modification is quick and dirty, use them at your own risk. If you find anything wrong, you can just remove the tsdk configuration, to switch back to vscode's built-in TypeScript.

警告:修改既快速又脏,使用它们的风险自负。如果你发现任何错误,你可以删除 tsdk 配置,切换回 vscode 的内置 TypeScript。