I know, TypeScript generate JavaScript file with equivalent code.
But, here is a problem, where TypeScript doesn't generate equivalent JavaScript code.
demo.ts
function foo()
{
if(1)
{
let myName = "Raktim";
}
console.log(myName);
}
foo();
demo.js
function foo() {
if (1) {
var myName = "Raktim";
}
console.log(myName);
}
foo();
See above, I declared a locale variable myName in the demo.ts file. But, look the JavaScript code it declare myName variable as global variable.
Summary: TypeScript generated JavaScript code's variable always are in global scope (in my case).
So, end of the code the meaning is changed. Why?
letcausesmyNameto be scoped to theif, so theconsole.logwon't be able to use it. The JS you shared wouldn't have that problem becausemyNamewould be hoisted intofoo's scope (not the global scope).