6

I'm looking to move a vanilla node.js project to TypeScript and I'm curious about how to restart my server with the latest changes without needing to run tsc from the command line each time I make a change.

I see from this answer two options: ts-node and tsc --watch

Can someone provide a little color on the difference between these two options? I understand they accomplish the same goal, but how are they different and in which situation should I use one over the other?

johnnyodonnell
  • 1,838
  • 3
  • 16
  • 34
  • 1
    See this comment on the answer: https://stackoverflow.com/questions/33535879/how-to-run-typescript-files-from-command-line/33536053#comment89243678_33536053 – Aankhen Jul 02 '18 at 10:21
  • @Aankhen that helps but it doesn't completely answer my question – johnnyodonnell Jul 02 '18 at 13:21
  • 1
    Fair enough… my experience is mostly with ts-node, but I’ve been told on IRC that you should avoid it because it adds another, brittle layer that doesn’t buy you much. I’ve been using it anyway because so far the convenience of not needing to compile my files, and getting line numbers in stack traces that refer to the TS files, is valuable and I haven’t yet encountered any ts-node–specific issues. – Aankhen Jul 02 '18 at 14:51
  • 1
    @Aankhen That was one of my concerns. I didn't want to add in another dependency if tsc already did the same thing. Though, getting line numbers in stack traces sounds like a pretty solid benefit over tsc. Hmm, I will have to think about this one. Thanks for your perspective! – johnnyodonnell Jul 02 '18 at 17:13

2 Answers2

4

You can add this script

"name_for_your_script" : "tsc <ts file name> --outFile <js file name> -w",

to your package.json file in scripts part and type this code

npm run name_for_your_script

into your terminal. this will automatically compile your ts codes when you save it

Amir Almian
  • 154
  • 6
1

tsc --watch (or tsc -w; same thing).

When your run that command, it will compile .ts files to .js when the .ts files are changed (via saving).

daCoda
  • 3,583
  • 5
  • 33
  • 38