The TypeScript code below compiles without errors:
class Something {
name: string;
constructor() {
name = "test";
}
}
Although this code compiles successfully, it mistakenly assumes that the `name` variable exists. However, when compiled to JavaScript, it will throw an error because I overlooked using the this
keyword:
/Users/cburtbrown/Documents/code/ts/js/tstest.js:6
console.log(name);
^
ReferenceError: name is not defined
at Something.action (/Users/cburtbrown/Documents/code/ts/js/tstest.js:6:21)
at Object.<anonymous> (/Users/cburtbrown/Documents/code/ts/js/tstest.js:10:25)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:456:32)
at tryModuleLoad (module.js:415:12)
at Function.Module._load (module.js:407:3)
at Function.Module.runMain (module.js:575:10)
at startup (node.js:159:18)
at node.js:444:3
If I make a typo in the variable within the constructor, I receive the following error:
Cannot find name 'namej'
Shouldn't this error be triggered even if the variable is spelled correctly?