typescript vscode 导入 import console = require("console"); 自动地
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53279182/
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
vscode imports import console = require("console"); automatically
提问by dragonsoul
import console = require("console");
console.
<< I type . and above gets imported automatically in VScode. Anybody knows how to disable that?
console.
<< 我打字。及以上在 VScode 中自动导入。有人知道如何禁用它吗?
(I assume it is one of my extensions. Probably Prettier.)
(我假设它是我的扩展之一。可能更漂亮。)
edit: it only happens in React Typescript environment. not in Typescript without react.
编辑:它只发生在 React Typescript 环境中。不是在没有反应的打字稿中。
回答by ZenVentzi
Disclaimer: this shouldn't be considered "the solution" but it's the simplest/fastest.
免责声明:这不应被视为“解决方案”,但它是最简单/最快的。
This answer is assuming you're using VSCode. Other IDEs should be similar.
这个答案假设您使用的是 VSCode。其他 IDE 应该类似。
- Start typing
console
- Click enter or type
.
, allowing IntelliSenseto addimport console = require("console");
- Ctrl+click (or F12, or Cmd+click on macOS) on
require("console")
- Comment out this code:
- 开始打字
console
- 单击 enter 或 type
.
,允许IntelliSense添加import console = require("console");
- Ctrl+单击(或 F12,或 Cmd+单击 macOS)
require("console")
- 注释掉这段代码:
declare module "console" {
export = console;
}
回答by KvD
I experienced this as well an it seems to be a problem with the Auto Import feature in VSCode. Disabling all extensions doesn′t seem to make it go away either.
我也遇到过这种情况,这似乎是 VSCode 中的自动导入功能的问题。禁用所有扩展似乎也不会让它消失。
As a workaround you can disable autoimports in settings.
作为解决方法,您可以在设置中禁用自动导入。
If you use Javascript
如果您使用 Javascript
"javascript.suggest.autoImports": false
“javascript.suggest.autoImports”:假
If you use Typescript
如果你使用打字稿
"typescript.suggest.autoImports": false
“typescript.suggest.autoImports”:假
EDIT: The faulty autoimport occurs because of this code in a package down the dependency tree
编辑:由于依赖关系树下的包中的此代码,发生错误的自动导入
declare module "console" {
export = console;
}
The package can be located in either your local node_modules directory or in a referenced package installed globally.
该包可以位于本地 node_modules 目录中,也可以位于全局安装的引用包中。
- Search your local node_modules for
declare module "console"
- If you find it in a local package, run
npm list [packageName]
to determine which package in package.json is dependent on the package with the console code in it.
- 在本地 node_modules 中搜索
declare module "console"
- 如果在本地包中找到它,请运行
npm list [packageName]
以确定 package.json 中的哪个包依赖于其中包含控制台代码的包。
If you don′t find code in your local node_modules you could either
如果你在本地 node_modules 中没有找到代码,你可以
Eliminate packages one by one in package.json
Search for the console code in globally installed modules which may be referenced by packages in your project
将package.json中的包一一剔除
在全局安装的模块中搜索控制台代码,这些模块可能会被您的项目中的包引用
%USERPROFILE%\AppData\Roaming\npm\node_modules %USERPROFILE%\AppData\Local\Microsoft\TypeScript
%USERPROFILE%\AppData\Roaming\npm\node_modules %USERPROFILE%\AppData\Local\Microsoft\TypeScript
I know it′s not a straight forward solution but I hope it helps, in my case I had a reference from react-native-copilot -> rimraf -> node which had the console code in it. Removing react-native-copilot solved the problem.
我知道这不是一个直接的解决方案,但我希望它有所帮助,就我而言,我有来自 react-native-copilot -> rimraf -> node 的参考,其中包含控制台代码。删除 react-native-copilot 解决了这个问题。
回答by Giorgio
If you add a snippet for inserting console.log
and use that instead, there will be no auto-import of "console"
如果您添加用于插入的代码段console.log
并改为使用它,则不会自动导入“控制台”
https://code.visualstudio.com/docs/editor/userdefinedsnippets#_create-your-own-snippets
https://code.visualstudio.com/docs/editor/userdefinedsnippets#_create-your-own-snippets
here is my snippet:
这是我的片段:
{
"Print to console": {
"prefix": "cl",
"body": [
"console.log('');",
],
"description": "Log output to console"
}
}
回答by Krzysztof Kaczor
The most elegant solution that I found is to create dummy console.d.ts
file somewhere in your project:
我发现的最优雅的解决方案是console.d.ts
在项目中的某处创建虚拟文件:
declare module 'console' {
export = typeof import("console");
}
This will prevent auto-importing.
这将阻止自动导入。
Credits: https://github.com/Microsoft/TypeScript/issues/30471#issuecomment-474963436
学分:https: //github.com/Microsoft/TypeScript/issues/30471#issuecomment-474963436
回答by FOLOF
If you like me forgets "cl", you can use multiple prefixes in snippets:)
如果你喜欢我忘记“cl”,你可以在片段中使用多个前缀:)
{
"Print to console": {
"prefix": ["cl","co","con","cons","conso","consol","console", "console.l","console.lo","console.log"],
"body": [
"console.log();",
],
"description": "Log output to console"
}
}
回答by Talin
One way to prevent this from happening is to modify your tsconfig.json file to limit the set of types that are automatically imported into your project.
防止这种情况发生的一种方法是修改 tsconfig.json 文件以限制自动导入到项目中的类型集。
I had this same problem, and I fixed it by adding:
我遇到了同样的问题,我通过添加以下内容修复了它:
types: []
types: []
into my tsconfig.json file. What this does is disable's TypeScript (and by extension VSCode) from automatically importing all node packages that being with @types/
into the project configuration. This doesn't prevent TS from importing those type definitions if you explicitly import a package using those types.
进入我的 tsconfig.json 文件。这样做是禁用 TypeScript(以及扩展 VSCode)自动将所有节点包导入@types/
到项目配置中。如果您使用这些类型显式导入包,这不会阻止 TS 导入这些类型定义。
In my specific case, the console
definition was coming from @types/node
, which had been imported into the project as a dependency of Storybook. However, my project was a webpack project, intended to run in a browser, so importing Node.js types into my source code made no sense. The base set of types that you would want to use in a browser are dom types, not node types.
在我的特定情况下,console
定义来自@types/node
,它已作为 Storybook 的依赖项导入到项目中。但是,我的项目是一个 webpack 项目,打算在浏览器中运行,因此将 Node.js 类型导入到我的源代码中是没有意义的。您希望在浏览器中使用的基本类型集是 dom 类型,而不是节点类型。
Depending on your project, you may have to explicitly add the set of base type packages into the types parameter (types: ["dom", "react"]
and so on). However, in my case this turned out to be unnecessary, my project was able to compile just fine with an empty list. And the tendency of VSCode to automatically import 'console' appears to have completely gone away; I haven't noticed any other ill effects so far.
根据您的项目,您可能必须将一组基本类型包显式添加到 types 参数(types: ["dom", "react"]
等等)。但是,在我的情况下,这被证明是不必要的,我的项目能够使用空列表进行编译。VSCode 自动导入“控制台”的趋势似乎已经完全消失;到目前为止,我还没有注意到任何其他不良影响。
More information on setting types in tsconfig.json here: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html
有关在 tsconfig.json 中设置类型的更多信息:https://www.typescriptlang.org/docs/handbook/tsconfig-json.html