为什么打字工具会故意创建重复的打字集?

Why does the typings tool deliberately create duplicate sets of typings?

正在阅读Angular 2 Quickstart I came across this section under TypeScript Configuration

Typing file collisions

The TypeScript compiler does not tolerate redefinition of a type. For example, it throws an error if it's given two definitions for the Promise type.

Double definitions are common. In fact, the typings tool deliberately creates duplicate sets of typings (for reasons best explained elsewhere). Look in the project structure for the typings folder where we should find something like:

typings
  browser
    ambient
      es6-shim
        es6-shim.d.ts
  main
    ambient
      es6-shim
        es6-shim.d.ts
  browser.d.ts
  main.d.ts

The es6-shim typings are duplicated and the browser.d.ts and main.d.ts have overlapping content.

如果我阅读 typings readme,它说:

If you're building a front-end package it's recommended you use typings/browser.d.ts. The browser typings are compiled by following the browser field overrides.

问题:

为什么 typings tool 故意创建重复的打字集?

为什么建议您使用 typings/browser.d.ts 作为前端包?

我的猜测是因为支持browser字段可以创建不同的类型?

如果是,browser 字段是什么?它以何种方式改变类型?

你猜对了。 Typings 认识到包在浏览器和其他位置上的功能可能不同。对于大多数常见的包来说,此功能是不必要的,因为同构 JS 很流行,并且许多包在所有 JS 环境中的功能都相同。

但是,如果我的代码故意检查环境的功能,或者如果我想阻止用户使用浏览器中损坏的功能,那么拥有它会很有帮助。

举个例子。我有一个颜色选择器包。这个包,如果我在桌面上,将允许我从屏幕上的任何地方选择一种颜色。显然这依赖于系统级的API,不能被浏览器使用。另一方面,我的浏览器 可以 在浏览器 window 中选择颜色。我没有发布一个全新的包,而是向程序员公开了不同的类型,以便他们知道根据他们的环境允许哪些功能。

最终,您几乎不需要花费任何费用(除了在您的计算机上存储 space 文件),而且还可以使用其他方式无法实现的潜在功能。