Consider this scenario, where I accidentally redefine the 'name' string:
function printName( name:string )
{
let name = "Oops!";
console.log(name);
}
In this case, I WANT a compile-time error. And, I get one. TypeScript gives "Duplicate identifier 'name'" at compile time. Life is good.
But now when I add try/catch, I no longer get an error.
function printName(name: string )
{
try
{
let name = "Oops!";
console.log("printName: " + name);
}
catch (e)
{
console.log("error:", e);
}
}
Why don't I get an error anymore? Is there anything I can do to still get the same error at compile-time?
EDIT: Apparently if I put the code inside any kind of block (such as if-else), I don't get an error when I redefine a parameter. Apparently TypeScript in this case just figures I want to make a new variable, in its own scope, so the inner name becomes name_1. Is there any way I can get a warning or error for this?