Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run TypeScript files from command line?

I'm having a surprisingly hard time finding an answer to this. With plain Node.JS, you can run any js file with node path/to/file.js, with CoffeeScript it's coffee hello.coffee and ES6 has babel-node hello.js. How do I do the same with Typescript?

My project has a tsconfig.json which is used by Webpack/ts-loader to build a nice little bundle for the browser. I have a need for a build step run from the console before that, though, that would use some of the .ts files used in the project to generate a schema, but I can't seem to be able to run a single Typescript file without compiling the whole project.

like image 599
Gunchars Avatar asked Nov 05 '15 02:11

Gunchars


People also ask

How do I open a TypeScript file?

To open it, try dragging it directly into the open program window or use the Media > Open File menu item. You can also change the program currently associated with . TS files, and set it as VLC. Another option for opening the TS file is to rename it to something that your existing media player will support, like .


1 Answers

How do I do the same with Typescript

You can leave tsc running in watch mode using tsc -w -p . and it will generate .js files for you in a live fashion, so you can run node foo.js like normal

TS Node

There is ts-node : https://github.com/TypeStrong/ts-node that will compile the code on the fly and run it through node 🌹

npx ts-node src/foo.ts 
like image 65
basarat Avatar answered Oct 06 '22 09:10

basarat