import console = require("console");
console.
<< I type . and above gets imported automatically in 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. not in Typescript without react.
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.
I had this same problem, and I fixed it by adding:
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.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.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.More information on setting types in tsconfig.json here: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html
If you like me forgets "cl", you can use multiple prefixes in snippets:)
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.
console
.
, allowing IntelliSense to addimport console = require("console");
require("console")
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.
As a workaround you can disable autoimports in settings.
If you use Javascript
"javascript.suggest.autoImports": false
If you use Typescript
"typescript.suggest.autoImports": false
EDIT: The faulty autoimport occurs because of this code in a package down the dependency tree
The package can be located in either your local node_modules directory or in a referenced package installed globally.
declare module "console"
npm list [packageName]
to determine which package in package.json is dependent on the package with the console code in it.If you don´t find code in your local node_modules you could either
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
%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.
If you add a snippet for inserting
console.log
and use that instead, there will be no auto-import of "console"https://code.visualstudio.com/docs/editor/userdefinedsnippets#_create-your-own-snippets
here is my snippet:
The most elegant solution that I found is to create dummy
console.d.ts
file somewhere in your project:This will prevent auto-importing.
Credits: https://github.com/Microsoft/TypeScript/issues/30471#issuecomment-474963436