So I'm a beginner learning typescript. I have some code like:
let num: number = 123;
console.log(123);
And when I use Node.js to run this file, which is saved as test.ts. I get SyntaxError: Unexpected token ':'.
As far as I understand, this is how we set the type of a variable in TS right? I'm using VSCode by the way, I have node and npm installed. I also have typescript installed. I verified by checking their versions. I can't find anything online for this problem because it's just so silly.
What am I doing wrong?
:numberand it will still understand thatnumis a number, as it infers the type from the assignment.node test.jsthen I could runnode test.ts. I guess I was wrong. My mistake was that I wrote JS code in TS then ran it as .TS but node in this case was actually running JS. Thank you for the clarificationtsxpackage it runs all typescript in node from ES3 to ESNext/NodeNext it's the superior alternative to tsnode you simply runyarn tsx script.tsor whatever your target path is if using npm it'snpx tsx <path to file>tsxpackage is an example of the compilers that simulate running TypeScript on-the-fly I was talking about, yep.