gpt4 book ai didi

javascript - 澄清 Typescript 的目标和库设置的功能

转载 作者:行者123 更新时间:2023-11-30 19:33:58 26 4
gpt4 key购买 nike

当tsconfig.json有如下内容

"target": "es5",
"lib": [ "es6", "dom", "es2017" ]

它似乎没有将 es2017 构造转换为 es5。例如,以下将在 IE11 上失败:

var foo = [1, 2, 3].includes(1);

这是设计使然还是我缺少 tsconfig.json 中的设置?

最佳答案

Clarification on functionality of Typescript's target and lib settings

我认为的简化方法是 target 说明输出 JavaScript 将具有的语法,而 lib 说明 API 成员 您的 TypeScript 源代码可以使用。这两个问题的答案中有更多细节:

...it doesn't seem to convert es2017 constructs to es5... Is this by design or am I missing a setting in tsconfig.json?

你是对的。这是设计使然。 TypeScript 转换为 target 语法;它不会填充 target 中缺少的 API 成员。 Here is a quote来自 TypeScript 团队的核心成员:

I think you're confusing transpilation with auto-polyfilling. TypeScript doesn't automatically polyfill for you like Babel does, but does perform syntactic downleveling (e.g. for arrow functions). If you want to use ES6 runtime prototype methods, I'd simply include an appropriate ES6 polyfill and its accompanying definition file.

如果您的 lib 包含 target 运行时中不存在的 API 成员(例如 Array.prototype.include),则您需要安装提供这些 API 成员的 polyfill。

关于javascript - 澄清 Typescript 的目标和库设置的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56140658/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com