需要澄清目标和 lib 编译器选项 [英] Need clarification of the target and lib compiler options

查看:29
本文介绍了需要澄清目标和 lib 编译器选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现我对 target 和 lib 选项以及它们如何与源代码中支持的功能进行交互感到困惑.我觉得文档需要改进一点,所以在提出问题之前先在这里询问.

I find I'm confused by the target and lib options and how they interact with the features supported in the source code. I feel the docs need improving a little so am asking here before raising an issue.

我天真地假设 target 指定了输出代码需要运行的 JS 版本(添加了模块加载器).因此,我们始终可以在我们的源代码中使用 TS 支持的所有高级 JS 功能(如对象传播),并且编译器会为我们指定的目标生成合适的代码.我假设它手头有 polyfills 等,代码只会在目标 VM 上运行.

I naively assumed that target specifies the version of JS that the output code requires to run (with the addition of a module loader). Thus we can always use all the advanced JS features (like object spread) that TS supports in our source and the compiler generates suitable code for the target we specify. I assume it had polyfills etc at hand and the code would just run on the target VM.

然而,lib 选项的文档指定默认库取决于目标.但是,libs 影响可用的源类型,从而影响我们可以使用的代码.因此,我们可以使用的源特征取决于目标.这不是我的预期.我应该说我对 lib 的理解是它们使用不同的 API 进行类型化,尽管文档并没有真正说明它们是什么.

However the docs for the lib option specify the default libs depend on the target. But, libs effect what source types are available and so effect what code we can use. Thus the source features we can use depend on the target. That is not as I expected. I should say my understanding of lib is that they are typings with a different API, though the docs do not really say what they are.

我可以看到这里有一些不依赖于类型的语言特性,而另一些则依赖于类型.然而,目前尚不清楚这是否是造成这种情况的部分原因.

I can see that here are some language features that do not depend on types and others that do. However it's not clear if that's part of the reason for this situation.

有人可以澄清一下吗?

第二个问题是为什么 ES6 和 ES2015 库通常被记录为同一个东西.

A secondary question is why is there both an ES6 and an ES2015 lib when they are usual documented as being the same thing.

谢谢

推荐答案

(这开始是一个评论,但它太长了.)

(This started as a comment but it got too long.)

这有点令人困惑,部分原因是它背后有一些历史.我没有资格权威地回答这个问题,但我从早期开发开始就一直关注 TypeScript,这是我的理解:

It's a bit confusing partly because there's some history behind it. I'm not qualified to answer this authoritatively but I've been following TypeScript since early development and this is my understanding:

  • --target 告诉编译器编译时要包含的库版本(例如,如果使用 PromiseES5 将给出编译器错误,但是 ES6 会知道所有关于 Promise) 编译器发出的 JS 版本(例如ES5 会向下编译类语法,但 ES6 会保留它.
  • --lib 是后来添加的,让您在编译时更好地控制要使用的库版本而无需更改发出的 JS 目标.例如,一个常见的问题是您可能包含 ES6 库功能的 polyfill,例如 Promise,但您希望通过向下编译 class 语法来定位 ES5 浏览器.在 --lib 出现之前,你要么必须以 ES6 为目标,以避免关于 Promise 的编译错误,然后再次向下编译 em> 使用 Babel,或者你可以针对 ES5 并为 Promise 提供你自己的类型定义,这样编译器就不会给你一个错误.现在使用 --lib 你可以简单地说你的 --target ES5--lib ES6,编译器不会抱怨 Promise 但仍然向下编译为 ES5.
  • 这两个选项都不会导致 TS 发出任何库 polyfill(Promise 等),正如您所发现的;您有责任提供正确的运行时库.它只发出一些低级语言兼容性帮助程序,例如 __extends__awaiter(区别在于 classasync 不仅仅是一个可以在运行时被 polyfill 的 API,它还是一个具有语法含义的语言特性).--lib 选项只是您根据您知道在运行时将拥有的内容来获得正确级别的编译检查的方式.
  • 至于为什么同时存在 ES6ES2015,那只是因为 ECMAScript 更改了名称,而 TS 保留了旧名称作为向后兼容的有效选项.:)
  • --target tells the compiler what library version to include while compiling (for example ES5 will give a compiler error if you use Promise, but ES6 will know all about Promise) and what version of JS is emitted by the compiler (for example ES5 will down-compile class syntax, but ES6 will leave it in).
  • --lib was added later to give you better control over what library version to use while compiling without changing the emitted JS target. For example, a common problem was that you may include polyfills for ES6 library features, such as Promise, but you want to target ES5 browsers by down-compiling class syntax. Before --lib was around you either had to target ES6 to avoid compile errors about Promise, then down-compile again using Babel, or you could target ES5 and provide your own type definition for Promise so that the compiler doesn't give you an error. Now with --lib you can simply say your --target ES5 and --lib ES6, and the compiler will not complain about Promise but still down-compile to ES5.
  • Neither option will cause TS to emit any library polyfills (Promise, etc), as you evidently found out; it's your responsibility to provide the correct runtime libraries. It only emits a few down-level language compatibility helpers, like __extends and __awaiter (the difference being that class or async is not just an API that can be polyfilled at runtime, it's a language feature with syntax implications). The --lib option is just your way of getting the right level of compile checking based on what you know you are going to have at runtime.
  • As for why there's both ES6 and ES2015, that's just because ECMAScript changed the name and TS left the old name as a valid option for backwards compatibility. :)

您会在这些 TS 问题中找到很多内容:

You'll find a lot of this covered in these TS issues:

TSConfig 参考链接:

TSConfig Reference links:

这篇关于需要澄清目标和 lib 编译器选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆