Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Typescript Type Annotations throws 'Unexpected token ":"'

I am trying to annotate my variables with types but when running the code it simply gives off an error:

let foo: number = 23;
console.log( foo );

// let foo: number = 23;
          ^

// SyntaxError: Unexpected token ':'
// [90m    at Object.compileFunction (node:vm:352:18)[39m
// [90m    at wrapSafe (node:internal/modules/cjs/loader:1031:15)[39m
// [90m    at Module._compile (node:internal/modules/cjs/loader:1065:27)[39m
// [90m    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)[39m
// [90m    at Module.load (node:internal/modules/cjs/loader:981:32)[39m
// [90m    at Function.Module._load (node:internal/modules/cjs/loader:822:12)[39m
// [90m    at Function.executeUserEntryPoint [as runMain] // (node:internal/modules/run_main:79:12)[39m
// [90m    at node:internal/main/run_main_module:17:47[39m

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You have two options:

First compile it to plain JS with tsc myfile.ts and run the JS file with node myfile.js.

Or npm install ts-node which lets you run TS files like you would run JS files: ts-node myfile.ts

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading