[英]vscode imports import console = require(“console”); automatically
import console = require("console");
console.
<< I type . “我打字。 and above gets imported automatically in VScode.
以上是在VScode中自动导入的。 Anybody knows how to disable that?
有谁知道如何禁用它?
(I assume it is one of my extensions. Probably Prettier.) (我认为这是我的扩展之一。可能比较漂亮。)
edit: it only happens in React Typescript environment. 编辑:它只发生在React Typescript环境中。 not in Typescript without react.
没有反应就没有打字稿。
Disclaimer: this shouldn't be considered "the solution" but it's the simplest/fastest. 免责声明:这不应被视为“解决方案”,但它是最简单/最快的。
This answer is assuming you're using VSCode. 这个答案假设你正在使用VSCode。 Other IDEs should be similar.
其他IDE应该类似。
console
console
.
.
, allowing IntelliSense to add import console = require("console");
import console = require("console");
require("console")
require("console")
上按Ctrl +单击(或F12,或Cmd +单击macOS require("console")
declare module "console" {
export = console;
}
I experienced this as well an it seems to be a problem with the Auto Import feature in VSCode. 我也经历过这种情况,这似乎是VSCode中自动导入功能的一个问题。 Disabling all extensions doesn´t seem to make it go away either.
禁用所有扩展似乎也没有让它消失。
As a workaround you can disable autoimports in settings. 作为解决方法,您可以在设置中禁用自动导入。
If you use Javascript 如果你使用Javascript
"javascript.suggest.autoImports": false “javascript.suggest.autoImports”:false
If you use Typescript 如果你使用Typescript
"typescript.suggest.autoImports": false “typescript.suggest.autoImports”:false
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目录中,也可以位于全局安装的引用程序包中。
declare module "console"
declare module "console"
npm list [packageName]
to determine which package in package.json is dependent on the package with the console code in it. 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 在package.json中逐个删除软件包
Search for the console code in globally installed modules which may be referenced by packages in your project 在全局安装的模块中搜索可能由项目中的包引用的控制台代码
%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. 我知道这不是一个直接的解决方案,但我希望它有所帮助,在我的情况下,我有一个来自react-native-copilot的参考 - > rimraf - >节点,其中包含控制台代码。 Removing react-native-copilot solved the problem.
删除react-native-copilot解决了这个问题。
If you add a snippet for inserting console.log
and use that instead, there will be no auto-import of "console" 如果您添加一个用于插入
console.log
的代码段并使用它,则不会自动导入“console”
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('$1');",
],
"description": "Log output to console"
}
}
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
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($1);",
],
"description": "Log output to console"
}
}
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: []
into my tsconfig.json file. 进入我的tsconfig.json文件。 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. 这样做是禁用TypeScript(和扩展名VSCode)自动将所有带有
@types/
节点包导入到项目配置中。 This doesn't prevent TS from importing those type definitions if you explicitly import a package using those 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. 在我的特定情况下,
console
定义来自@types/node
,它已作为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. 但是,我的项目是一个webpack项目,旨在在浏览器中运行,因此将Node.js类型导入我的源代码是没有意义的。 The base set of types that you would want to use in a browser are dom types, not node types.
您希望在浏览器中使用的基本类型集是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). 根据您的项目,您可能必须将类型基本类型包的显式添加到types参数中(
types: ["dom", "react"]
等)。 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;
VSCode自动导入“控制台”的趋势似乎完全消失了; I haven't noticed any other ill effects so far.
到目前为止,我还没有注意到任何其他不良影响。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.