简体   繁体   English

TypeScript导入混乱-很难做到

[英]TypeScript Import Confusion - It Can't Be This Hard

I have been working with TypeScript off and on for two years now. 我已经开始使用TypeScript了两年了。 I am not an expert by any means, but I've spent some time in the eco-system: with VS2015 and node tools, with VSCode, and at the command line with tsc and typings (also used by VSCode). 我无论如何都不是专家,但是我花了一些时间在生态系统中:使用VS2015和节点工具,使用VSCode,以及在命令行使用tsc和键入(也被VSCode使用)。

And I have struggled with the correct way to get static typing and auto-complete etc in the code I've been writing. 我一直在努力尝试用正确的方法在我编写的代码中获得静态类型和自动完成等功能。

I have a repo I maintain (EasyNodeQ) which was the start of my TypeScript experience and I sort of got that to a manageable place with ///reference and DefinitelyTyped. 我有一个维护的仓库(EasyNodeQ),这是我TypeScript体验的开始,我使用/// reference和DefinitelyTyped将其存储到一个易于管理的地方。 But any time I tried to use that within another project I had issues. 但是任何时候我试图在另一个项目中使用它时,我都会遇到问题。

Things got a little better when I started using typings (rather than downloading the *.d.ts files myself) and especially with the ambient flag. 事情变得更好一点 ,当我开始使用分型(而非下载* .d.ts文件我自己),特别是与环境标志。

But I still have lots of issues trying to use that package within other projects. 但是我在尝试在其他项目中使用该程序包时仍然遇到很多问题。 Depending on the approach I take, I get lots of Duplicate Identifier's, or module not found's or... 根据我采用的方法,我会得到很多重复标识符,或者找不到模块或...

And this can range from Node definitions, to packages I use in both places (like node-uuid). 范围从Node定义到我在两个地方都使用的包(例如node-uuid)。

All I want to understand is this: how to I structure EasyNodeQ so that as I'm working on it, I get the static typings benefits of TypeScript but also have it be seamlessly included in other projects which can then also get these benefits? 我要了解的是:我如何构造EasyNodeQ,以便在我进行工作时,既可以得到TypeScript的静态类型输入好处,又可以将其无缝地包含在其他项目中,然后还可以得到这些好处?

Does that make sense? 那有意义吗?

The basic use cases are: npm install a package and get its typings, use my EasyNodeQ package and get its typings, work in a new project that uses EasyNodeQ and other packages and easily manage those typings. 基本用例是:npm安装软件包并获取其类型,使用my EasyNodeQ软件包并获取其类型,在使用EasyNodeQ和其他软件包的新项目中进行工作,并轻松管理这些类型。

Preferably in a VSCode or command line way... 最好以VSCode或命令行方式...

This is with ES6 and the latest version of TypeScript (though an answer that works with ES5 would be nice - just not required). 这是与ES6和最新版本的TypeScript一起使用的(尽管与ES5配合使用的答案会很好-只是不需要)。

I hope this makes sense. 我希望这是有道理的。 I've looked all over and I can't cobble together an answer that works. 我四处张望,无法拼凑出有效的答案。

UPDATE 更新

I'm not convinced I've done this the "right" way, but it's working now so I thought I'd post the various things I've done (generally in the order I think they mattered and not inclusive because I may have forgotten some). 我不敢相信我已经按照“正确”的方式进行了操作,但是它现在可以正常工作了,所以我认为我会发布我所做的各种事情(通常按照我认为它们很重要但不具有包容性的顺序,因为我可能忘了一些)。

  • I hadn't npm'd the dependency package (easynodeq) and was just using a git url in the package.json - so I created a proper npm package and now install that package from npm 我没有npm'd依赖包(easynodeq),只是在package.json中使用了git url-所以我创建了一个正确的npm包,现在从npm安装该包
  • Instead of trying to use Bus.ts as both code and definition, I made Bus.js the "meat" of the npm, and built a Bus.d.ts file (also in the npm) 我没有尝试将Bus.ts用作代码和定义,而是将Bus.js用作npm的“肉”,并构建了Bus.d.ts文件(也在npm中)
  • Embraced typings, using non-ambient definitions where possible and a mix of ambient definitions downloaded (via git) from DefinitelyTyped and "--ambient" definitions for the rest (because I'm still confused about the difference). 拥抱类型,尽可能使用非环境定义,并从DefinitelyTyped下载环境定义(通过git)和其余的“-环境”定义混合使用(因为我仍然对区别感到困惑)。 The ambient definitions ended-up being the majority: node, express, serve-static, express-serve-static-core, mime, amqplib, when vs just bluebird and node-uuid, even though most of them were found by "typings search ..." Am I doing something wrong? 最终,环境定义占大多数:node,express,serve-static,express-serve-static-core,mime,amqplib,而与bluebird和node-uuid相比,即使其中大多数是通过“类型搜索”找到的...“ 难道我做错了什么?
  • Modified package.json to also do "typings install" 修改后的package.json也可以进行“类型安装”
  • Cleaned-up the git repo 清理git仓库

There are several ways to make this work today. 今天有几种方法可以使这项工作。 As you mentioned, using Typings works for definitions that aren't natively included in their NPM packages. 如您所述,使用Typings可以处理其NPM软件包中未包含的定义。 For ones that do, you can using the typings field in package.json and it'll work with node module resolution. 对于这种情况,您可以使用package.jsontypings字段,它将与节点模块解析一起使用。 When you combine this together, you can publish packages that use both typings.json and relies on packaged typings - though this now forces your consumers to be using Typings to install the definitions. 将它们组合在一起时,您可以发布同时使用typings.json和依赖打包类型的程序包-尽管现在这迫使您的使用者使用Typings来安装定义。 None of this, however, works with "ambient" definitions as they can not be namespaced properly. 但是,这些都不能与“环境”定义一起使用,因为它们无法正确命名空间。

This may be useful: https://github.com/typings/typings/blob/master/docs/faq.md#should-i-use-the-typings-field-in-packagejson . 这可能很有用: https : //github.com/typings/typings/blob/master/docs/faq.md#should-i-use-the-typings-field-in-packagejson There's also dozens of examples I have using both workflows: https://github.com/blakeembrey/change-case/blob/master/package.json#L6 which uses node module resolution all the way down and https://github.com/blakeembrey/popsicle/blob/master/typings.json which uses Typings instead. 我还使用了两个工作流程,也有很多示例: https : //github.com/blakeembrey/change-case/blob/master/package.json#L6一直使用节点模块解析,而https:// github。 com / blakeembrey / popsicle / blob / master / typings.json ,它改用Typings。 Using Typings is only possible because it works to create namespaced ambient modules for you, but they won't conflict. 仅可能使用Typings,因为它可以为您创建命名空间的环境模块,但它们不会冲突。

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

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