我在編譯這段特定的代碼時遇到了問題(這是來自Angular2項目)。Typescript編譯器忘記添加括號?
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 version 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代碼,但它似乎編譯器不添加必要的括號內爲其他區塊的內容。
據我所知,這很可能是由於我沒有在我的Typescript代碼中添加這些括號,因爲else語句包含一個單獨的塊(使括號不必要,請糾正我,如果我錯了) 。
但是,在Javascript中,else塊(單個for Typescript中的循環)輸出到多個語句。
你甚至可以看到indendation,我認爲這是有道理的,以下是聲明_loop_1變量的第一個兩條指令應包含在這個else塊以及。
我明明可以通過簡單地在我的打字稿代碼添加括號解決這個問題(我的理解它,也許可以說是更好的做法)。
是不是把這些括號放錯了,還是這是我應該報告的編譯器的問題?
注意:英語不是我的主要語言。
我認爲你可以添加一個問題[給Github上的TS團隊](https://github.com/Microsoft/TypeScript/issues)。 :) – Paleo