Typescript编译器忘记添加括号?

我在编译这段特定代码时遇到了问题(这是来自Angular2项目).

public reloadRecords() {
    let step = (this.timeInterval.max - this.timeInterval.min) / this.recordsChartSteps;
    let data = new Array(this.recordsChartSteps);
    let labels = new Array(this.recordsChartSteps);
    let doneCount = 0;
    let done = new EventEmitter();

    done.subscribe(() => {
        this.recordsChartData[0].data = data;
        this.recordsChartLabels = labels;
    });

    if (this.timeInterval.min == 0)
        this.data.getRecordCount(this.timeInterval.min, this.timeInterval.max).subscribe(count => {
            data[data.length - 1] = count;
            labels[labels.length - 1] = "Total";

            done.emit();
        });
    else for (let i = 0; i < this.recordsChartSteps; i++) {
        let min = this.timeInterval.min + step * i;
        let max = min + step - 1;

        this.data.getRecordCount(min, max)
            .subscribe(count => {
                data[i] = count;
                labels[i] = "De " + new Date(min).toLocaleTimeString() + " à " + new Date(max).toLocaleTimeString();

                if (++doneCount >= this.recordsChartSteps) done.emit();
            }); 
        }
}

使用typescript版本2.0.10(从npm开始),这是我得到的输出.

GlobalViewComponent.prototype.reloadRecords = function () {
    var _this = this;
    var step = (this.timeInterval.max - this.timeInterval.min) / this.recordsChartSteps;
    var data = new Array(this.recordsChartSteps);
    var labels = new Array(this.recordsChartSteps);
    var doneCount = 0;
    var done = new core_1.EventEmitter();
    done.subscribe(function () {
        _this.recordsChartData[0].data = data;
        _this.recordsChartLabels = labels;
    });
    if (this.timeInterval.min == 0)
        this.data.getRecordCount(this.timeInterval.min, this.timeInterval.max).subscribe(function (count) {
            data[data.length - 1] = count;
            labels[labels.length - 1] = "Total";
            done.emit();
        });
    else
        var _loop_1 = function(i) {
            var min = this_1.timeInterval.min + step * i;
            var max = min + step - 1;
            this_1.data.getRecordCount(min, max)
                .subscribe(function (count) {
                data[i] = count;
                labels[i] = "De " + new Date(min).toLocaleTimeString() + " à " + new Date(max).toLocaleTimeString();
                if (++doneCount >= _this.recordsChartSteps)
                    done.emit();
            });
        };
        var this_1 = this;
        for (var i = 0; i < this.recordsChartSteps; i++) {
            _loop_1(i);
        }
};

它是有效的Javascript代码,但似乎编译器没有为else块的内容添加必要的括号.

我理解这很可能是因为我没有在我的Typescript代码中添加这些括号,因为else语句包含一个块(不需要括号,如果我错了请纠正我).

但是,在Javascript中,else块(Typescript中的单个for循环)输出到多个语句.

你甚至可以看到压痕,我认为在声明_loop_1变量的第一个指令之后的两个指令应该包含在这个else块中也是有意义的.

我显然可以通过简单地在我的Typescript代码中添加括号来解决这个问题(据我所知,这可能是更好的做法).

我不应该把这些括号,或者这是我应该报告的编译器的问题?

注意:英语不是我的主要语言.

最佳答案 是的,这看起来像一个bug,你应该报告它.这是一个快速简化的复制:

var test = false;
if (test) for (let i = 0; i < 10; i++) {
  let x = () => i;
}

Playground link.

该代码绝对应该什么都不做,如果你在我的本地Chrome中运行它,那么它就是这样做的.如果你通过TypeScript运行它,而不是抛出’Uncaught TypeError:_loop_1不是一个函数’.很棒的发现!

点赞