-->

Why does the typescript transpiler not hoist varia

2019-05-25 04:07发布

问题:

I understand that ES6 and TypeScript both support block level scoping, but when targeting ES3 and ES5 the output should be function level scoping. I think there has to be a logic behind why TypeScript isn't hoisting variables.. and I'm not running into a problem, I'm more just curious why it doesn't hoist variables.

For example, given the following TypeScript:

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

The transpiler outputs:

function seed(length, multiplier) {
  var result = [];
  for (var i = 0; i < length; i++) {
    var n = i * multiplier;
    result.push(n);
  }
  return result;
}

The result I would have expected would be one with the variable declarations hoisted to the top of the function. Looking something like this:

function seed(length, multiplier) {
  var
    i, n,
    result = [];

  for (i = 0; i < length; i++) {
    n = i * multiplier;
    result.push(n);
  }
  return result;
}

Any insight is greatly appreciated. Thanks!

回答1:

It's because the compiler doesn't output code based on a coding standard. It tries to be as close as possible to the original input.

Note that var variables are hoisted behind the scenes anyway (var hoisting). There's not a need for the TypeScript compiler to change the input in this case and doing so would needlessly increase its complexity.



回答2:

I think there has to be a logic behind why TypeScript isn't hoisting variables.. and I'm not running into a problem, I'm more just curious why it doesn't hoist variables.

Exactly that. Because it isn't a problem, why go though the effort of hoisting?

Also in case of let It would actually be wrong to hoist it the way you showed.

Temporal Dead Zone

Firstly let variables cannot be used before declaration like var variables could be. With var you would get undefined. With let you would get a ReferenceError due to temporal deadzone. Fortunately TypeScript will give you a compile time error anyways so you don't run into that at runtime :

console.log(a); // Error: Block scoped variable used before declaration 
let a = 123;

Block Scoping

let variables declared in a for are only available in the for loop. This is because they are block scoped and not function scoped. More on this

Again TypeScript will give you a compile time error if you misuse let (instead of you running into it at runtime):

for(let i: number = 0; i < 10; i++) {    
}  
console.log(i); // Error: no variable i in scope

Hope that helps