TypeScript 在tslib导入上悬挂的变量声明很糟糕,

mctunoxg  于 3个月前  发布在  TypeScript
关注(0)|答案(3)|浏览(38)

TypeScript版本: 3.2.0-dev.20181204
搜索词:
代码

export default async function foo() {
  const out = [];
  for await (const item of [1, 2]) {
    out.push(item);
  }

  return out;
}

foo().then(console.log);

预期行为:

TS将变量声明放在正确的作用域中。

实际行为:

变量 _b 在兄弟作用域中被使用,尽管它没有在任何父作用域中声明。这只发生在 importHelpers 启用时。当它被禁用时,变量将在正确的作用域中被声明。

import * as tslib_1 from "tslib";
export default async function foo() {
    var e_1, _a;
    const out = [];
    try {
        // _b is declared in wrong scope, because it is later used inside
        // the finally block
        for (var _b = tslib_1.__asyncValues([1, 2]), _c; _c = await _b.next(), !_c.done;) {
            const item = _c.value;
            out.push(item);
        }
    }
    catch (e_1_1) { e_1 = { error: e_1_1 }; }
    finally {
        try {
            if (_c && !_c.done && (_a = _b.return)) await _a.call(_b);
        }
        finally { if (e_1) throw e_1.error; }
    }
    return out;
}
foo().then(console.log);

实验链接:

很难做到,这里是一个最小的仓库,可以在其中重现问题: https://github.com/marvinhagemeister/ts-helper-bug

相关问题:

vptzau2j

vptzau2j1#

因为这个被声明为 var,所以声明会被提升到函数的顶部,并应该继续正常工作。这个发射是否导致了问题?

nlejzf6q

nlejzf6q2#

盲目猜测:谷歌闭包编译器正在抱怨它

r1wp621o

r1wp621o3#

我偶然发现了这个问题,通过一个babel插件。而不是告诉每个插件遇到这个问题需要自己实现bar提升,这并不是很好。我认为当这里修复后,对整个生态系统来说会减少很多工作量。

相关问题