Typescript compiler forgets to add brackets?

I have a problem compiling this particular piece of code (this is from an Angular2 project).

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();
            }); 
        }
}

Using typescript version 2.0.10 (from npm), this is the result I get.

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);
        }
};

Valid Javascript code, however it seems that the compiler has not added the necessary brackets for the contents of the else block.

I understand that this is most likely due to the fact that I did not add these brackets to my typescript code, since the else statement contains one block (which makes the brackets unnecessary, please correct me if I am wrong).

However, in Javascript, the else block (one for the loop in Typescript) outputs to several statements.

, , , , , _loop_1, else.

, , , typescript (, , , , ).

, , ?

NB: .

+4
1

, , . :

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

.

, , Chrome. TypeScript, "Uncaught TypeError: _loop_1 ". !

+2

Source: https://habr.com/ru/post/1662396/


All Articles