Exploring AngularJS 2 and Typescript led me to create something using these technologies as a way to grasp the basics of Typescript. Through various sources, I delved into modules, Typescript concepts, with one particularly interesting topic discussing the differences between 'let' and 'var' when declaring variables. According to a query on Stack Overflow, the Typescript code snippet below was expected to show only one alert and throw an error in the console:
test.ts:
for(let i = 0; i < 1; i++) {
alert(i);
}
alert(i);
Compiled test.js:
for(var i = 0; i < 1; i++) {
alert(i);
}
alert(i);
//# sourceMappingURL=test.js.map
However, contrary to expectations, the compiler seems to ignore the "let" keyword and instead converts it to "var". This raises the question of why this discrepancy exists. Does Typescript function correctly only within classes?
My setup involves AngularJS configuration for 'npm start', which automatically compiles my 'test.ts' file:
"scripts": {
"start": "tsc && concurrently \"npm run tsc:w\" \"npm run lite\" ",
"lite": "lite-server",
"postinstall": "typings install",
"tsc": "tsc",
"tsc:w": "tsc -w",
"typings": "typings"
},