TypeScript 的“lib"是什么?选项真的吗? [英] What does the TypeScript "lib" option really do?

查看:20
本文介绍了TypeScript 的“lib"是什么?选项真的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然无法为此找到好的答案.目标"选项定义了结果将在哪个版本的 Javascript 上运行.lib"选项在任何地方都不太清楚.似乎它是描述目标环境的更细粒度的方式,但它会影响您可以在 .ts 源文件中写入的内容,这似乎很奇怪.认为 TS 是 JS 的超集,那么为什么它会影响例如 Promise() 是否可用?这似乎不仅定义了目标,而且还会影响您在 Typescript 中可用的功能.有人可以清楚地解释或直接给出答案吗(typescriptlang.org 或我看过的书籍中没有,例如指定要包含在编译中的库文件",这完全没有解释.

I have still not been able to find good answer for this. The "target" option defines, what version of Javascript the result will run on. The "lib" option is less clearly described anywhere. Seems like it is a more granular way to describe the target environment, but then it seems strange it affects what you can write in the .ts source files. Thought TS what as superset of JS, so why does it affect whether, e.g., Promise() is available or not? This seems like it does not only define the target but also affect what functions you have available in Typescript. Can someone explain that clearly or direct to an answer (there is none at typescriptlang.org or in the books I have looked at, e.g., "Specify library files to be included in the compilation", which explains absolutely nothing.

推荐答案

Typescript 没有任何内置类型 所有类型都来自一组基本定义(位于打字稿安装目录).默认情况下,target 定义了包含哪些 libs.例如 docs 状态:

Typescript does not have any built-in types all types come from a set of base definitions (located in the lib folder in the typescript install directory). By default the target defines which libs are included. For example the docs state:

注意:如果未指定--lib,则会注入默认的库列表.注入的默认库是:

Note: If --lib is not specified a default list of librares are injected. The default libraries injected are:

► 对于 --target ES5: DOM,ES5,ScriptHost

► 对于 --target ES6: DOM,ES6,DOM.Iterable,ScriptHost

基本思想是,虽然目标是处理语言特征(更具体地说,哪些语言特征需要向下编译,例如:for-of 或箭头函数),lib 选项处理运行时环境有哪些设施(即内置对象是什么样的,它们是什么).

The basic idea is that while target is deals with language features (more specifically which language features need to be down compiled, ex: for-of, or arrow functions), the lib option deals with what facilities the runtime environment has (ie. what built-in objects look like, what they are).

理想情况下,应该使用给定 target 的默认 libs.然而,我们可能有一个支持一些运行时设施但不支持语言特性的环境,或者我们可能以较低的 es 版本为目标运行时并填充一些运行时设施,这可以通常为某些事情完成(例如:承诺).

Ideally the default libs for a given target should be used. We may, however, have an environment which supports some of the runtime facilities but not the language features, or we may target runtime with a lower es version and poly-fill some of the runtime facilities, which can be in general done for some things (ex: Promises).

这篇关于TypeScript 的“lib"是什么?选项真的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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