Currently, I am delving into learning Typescript and have encountered a snag in my code. Despite searching extensively for a solution, I have been unable to find any relevant material pertaining to my issue. Below is the code snippet in question:
<code>
class Hello{
lars: string;
constructor(name: string) {
this.lars = name;
}
sayHello(){
return `hello ${this.lars}`;
}
}
let b = new Hello('Metallica');
</code>
Upon compiling the code using 'tsc test.ts', it compiles without any errors. However, upon running it with 'node test.ts', an error is thrown:
<blockquote>
lars: string;
^
SyntaxError: Unexpected token :
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:616:28)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)
at bootstrap_node.js:609:3
</blockquote>
The file runs when using 'Node test.js', but unfortunately does not yield the expected output of "Hello Metallica", while 'node test.ts' fails.
Here is the compiled code for reference:
var Hello = /** @class */ (function () {
function Hello(name) {
this.lars = name;
}
Hello.prototype.sayHello = function () {
return "hello " + this.lars;
};
return Hello;
}());
var b = new Hello('Metallica');