0

I created "test.ts" in VSCode like this:

var data ="https://a.b.c/d/e/f";
var url = new URL(data);
console.log(url.protocol);

and when I press F5 outputs "https:". But If I add the type like

var url:URL = new URL(data);

and press F5, the output is:

Process exited with code 1
C:\Program Files\nodejs\node.exe .\test.ts
Uncaught c:\[path]\test.ts:2
var url:URL = new URL(data);
       ^
SyntaxError: Unexpected token ':'
No debugger available, can not send 'variables'

What is wrong? If I search for the syntax, I can see an example like:

enter image description here

1
  • Node does not understand typescript syntax. Commented Aug 15, 2021 at 14:49

1 Answer 1

1

You cannot run your typescript code with node, you need to transpile it to Javascript. you can use tsc to do this and then run the transpiled javascript code using node.

Or you can use ts-node to run the typescript file directly. you can use npm install ts-node to install. This is a less efficient method and I don't recommend it for production runtime.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.