While ES6 and TypeScript both offer block level scoping, it is worth noting that when targeting ES3 and ES5, the output will default to function level scoping. I find it interesting that TypeScript does not hoist variables and am curious about the reasoning behind this decision.
For instance, consider the following TypeScript code:
function seed(length: number, multiplier: number): number[] {
let result: number[] = [];
for(let i: number = 0; i < length; i++) {
let n: number = i * multiplier;
result.push(n);
}
return result;
}
Upon transpiling, the output would be:
function seed(length, multiplier) {
var result = [];
for (var i = 0; i < length; i++) {
var n = i * multiplier;
result.push(n);
}
return result;
}
I had expected the variable declarations to be hoisted to the top of the function, creating a structure like this:
function seed(length, multiplier) {
var
i, n,
result = [];
for (i = 0; i < length; i++) {
n = i * multiplier;
result.push(n);
}
return result;
}
If you have any insights on this behavior, I would greatly appreciate it. Thank you!