I am encountering a problem with compiling a specific section of code in my 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();
});
}
}
The issue I'm facing is with the output from typescript version 2.0.10 (installed via 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);
}
};
The code is valid Javascript, but it appears that the compiler did not include the necessary brackets for the contents of the else block.
I believe this may be due to omitting the brackets in my Typescript code since the else statement consists of a single block. Should I have included brackets here or is this an issue with the compiler?
In the resulting Javascript, the else block (which is a single for loop in Typescript) translates into multiple statements.
The indentation suggests that the subsequent instructions following the declaration of the _loop_1 variable should also be part of the else block.
To resolve this issue, I could simply add brackets to my Typescript code, which might be considered better practice. Was it my mistake for not including those brackets, or is this a bug in the compiler that should be reported?
Note: English is not my primary language.