While TypeScript is supposed to generate JavaScript files with equivalent code, there can be instances where this is not the case.
For example, in the demonstration below:
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();
In the above code snippet, a local variable myName is declared in the demo.ts file. However, when we look at the compiled JavaScript code, it declares myName as a global variable instead.
This inconsistency can lead to unintended consequences, such as changing the meaning of the code. So why does this happen?