简体   繁体   English

从Visual Studio Code中的NPM包自动导入

[英]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. 我只是想通过VS Code中的intellisense自动导入TypeScript,就像C#在Visual Studio中使用了很长时间一样。 Using TypeScript Importer this works for my custom classes, but not for installed npm packages. 使用TypeScript Importer,这适用于我的自定义类,但不适用于已安装的npm软件包。

Simple example: linq is installed, I want to use Enumerable . 简单的例子: linq已安装,我想使用Enumerable I dont get any intellisense, VS Code says Cannot find name Enumerable . 我没有任何智能,VS Code说找不到名称Enumerable Only after manually typing import * as Enumerable from 'linq' it works. 只有在手动输入import * as Enumerable from 'linq'它才有效。 I also tried other addons like Auto Import or Auto Import ES6/TS none worked. 我还尝试了其他插件,例如“ 自动导入”或“ 自动导入ES6 / TS”,均无效

How can I make this work? 我该如何进行这项工作?

Note that package @types/linq for typings seems not required: 请注意,似乎不需要使用@types/linq软件包:

npm WARN deprecated @types/linq@2.2.33: This is a stub types definition for Linq.JS ( https://linqjs.codeplex.com/ ). 不推荐使用npm WARN @ types / linq @ 2.2.33:这是Linq.JS( https://linqjs.codeplex.com/ )的存根类型定义。 Linq.JS provides its own type definitions, so you don't need @types/linq installed! Linq.JS提供了自己的类型定义,因此您不需要安装@ types / linq!

Depending on the type of npm package installed you may or may not need a separate typings package. 根据安装的npm软件包的类型,您可能需要也可能不需要单独的键入程序包。 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. 作为示例,您应该看到在es6模块中定义的自己的类型出现在inteli-sense中。 Make sure the tsconfig.json is properly configured. 确保tsconfig.json已正确配置。 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. 请查阅此处的文档以确保一切均已正确配置。使用最新版本的vs-code,您不需要任何插件。 AutoImport works out of the box. AutoImport开箱即用。 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. 另外,如果linq库本身提供了类型定义,请尝试删除linq类型库。 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. 在您的linq示例中,我认为它不起作用的原因是因为linq不会将Enumerable类型导出为默认导出,或者程序包未在其索引文件中导出Enumerable类型。 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. 您可以很好地称呼您的枚举“ FooEnumerable”,它将仍然可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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