Assuming you understand that your code is equivalent:
var a, b b = a + 10 a = 10 alert(b.toString())
This, in turn, is equivalent to:
var a = undefined, b = undefined b = a + 10 a = 10 alert(b.toString())
The reason this should be allowed is because undefined is a valid value for a variable that you can assign and read.
There are various use cases when this functionality is valuable. For example, the module template used in typescript:
module x{ export var foo; }
Generates javascript code that uses this fact:
var x; (function (x) { x.foo; })(x || (x = {}));
This remains in TypeScript due to JavaScript backward compatibility (not to mention that it is useful).
Here is a pure TypeScript usecase. Perhaps you want to pass undefined to a function call ( this is really typescript ):
var a:number = undefined;
It is assumed that the TypeScript developer wants to know the core JavaScript language.
This does not apply to languages where Reading before assignment is not valid (e.g. C #). In C #, an unrecognized variable doesn't matter. In JavaScript, this is done. Therefore, TypeScript should allow this.
basarat
source share