为什么默认情况下不启用“downlevelIteration"? [英] Why `downlevelIteration` is not on by default?

查看:28
本文介绍了为什么默认情况下不启用“downlevelIteration"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当目标 ES5 使用扩展运算符 ...Iterator 转换为 Array 时,它显示使用 -downlevelIteration 编译器选项.一旦开启,价差操作符似乎就可以完美地工作.

When targeting ES5 with usage of spread operator ... to convert an Iterator to an Array, it shows the error to use -downlevelIteration compiler option. Once it is on, spread operators seem just work flawlessly.

我想知道为什么需要指定这个?除了从 tslib 添加更多发出的生成代码之外,启用它是否有任何缺点/限制?

I wonder why is there a need to specify this? Are there any downsides/limitation when it is enabled besides adding more emitted generated code from tslib?

另一个例子:动态创建 N 个数组(例如 3):

Another example: Dynamically create array of N (eg. 3):

[...Array(3).keys()]  // output: [0, 1, 2]

它在 VS Code 中显示错误:

It displays an error in VS Code:

来自 tsc 的错误信息:

类型 'IterableIterator' 不是数组类型或字符串类型.使用编译器选项--downlevelIteration"来允许迭代器迭代.

Type 'IterableIterator' is not an array type or a string type. Use compiler option '--downlevelIteration' to allow iterating of iterators.

编辑并查看代码和 TypeScript Playground 中的错误

推荐答案

阅读完发行说明 和文章 TypeScript 中 ES3/ES5 的下层迭代,我相信这个问题的答案是 downlevelIteration 被禁用,因为你需要决定(通过配置)你如何希望 TypeScript 处理兼容性代码的编译(以支持旧版本的 Javascript).

After reading the release notes and the article Downlevel Iteration for ES3/ES5 in TypeScript, I believe the answer to this question is that downlevelIteration is disabled because you need to decide (via configuration) how you want TypeScript to handle the compilation of compatibility code (to support older versions of Javascript).

正如文章中更冗长的解释所表明的那样,您必须决定是否希望 TypeScript 内联必要的辅助函数(简单,但可能会导致生产包尺寸更大),或者您是否希望配置 TypeScript使用 tslib 作为依赖项,然后调用其外部方法.

As the more lengthy explanation in the article makes clear, you have to make a decision as to if you want TypeScript to inline necessary helper functions (simple, but can result in larger production bundle size) or if you wish to configure TypeScript to use tslib as a dependency and then make calls to its external methods.

我强烈推荐阅读 TypeScript 中 ES3/ES5 的下层迭代 以获得更深入的理解……并且可能是您最初问题的替代解决方案.

I highly recommend reading Downlevel Iteration for ES3/ES5 in TypeScript for a deeper understanding… and possibly an alternate solution to your initial issue.

这篇关于为什么默认情况下不启用“downlevelIteration"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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