简体   繁体   中英

Auto Import from NPM packages in Visual Studio Code

I simply want automatically imports through intellisense in VS Code for TypeScript like C# has since a long time in Visual Studio. Using TypeScript Importer this works for my custom classes, but not for installed npm packages.

Simple example: linq is installed, I want to use Enumerable . I dont get any intellisense, VS Code says Cannot find name Enumerable . Only after manually typing import * as Enumerable from 'linq' it works. I also tried other addons like Auto Import or Auto Import ES6/TS none worked.

How can I make this work?

Note that package @types/linq for typings seems not required:

npm WARN deprecated @types/linq@2.2.33: This is a stub types definition for Linq.JS ( https://linqjs.codeplex.com/ ). Linq.JS provides its own type definitions, so you don't need @types/linq installed!

Depending on the type of npm package installed you may or may not need a separate typings package. If the package you install is published as a module (not a global) and it has type definitions present then the import should just work. As an example, you should see your own types defined in es6 modules appear in inteli-sense. Make sure the tsconfig.json is properly configured. Please consult the docs here to make sure everything is configured properly With the latest version of vs-code you do not need any plugins. AutoImport works out of the box. Try to remove all import plugins and try again. Also, try to remove the linq typings libraries if the linq library itself provides the type definitions. I use the auto-import feature all the time with my angular projects and it works quite well. In your linq example, the reason why I think it's not working is because linq doesn't export the Enumerable type as default export or the package does not export the Enumerable type in its index file. Then "*" import is just a workaround where you alias the global object to something. You can very well call your enumerable "FooEnumerable" it will still work fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM