Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run TypeScript files directly without generating any JavaScript files?

Tags:

typescript

In TypeScript, you'd use the following command to enter watch mode:

tsc -w -p .

Then when tsc does detect a change, it'll transpile the .ts files and generate .js files.

This means that you can run the new JavaScript files using:

node example.js

I'd like to run TypeScript files directly, without having to use node example.js, how can I do this?

like image 613
user3736174 Avatar asked Aug 29 '16 00:08

user3736174


People also ask

How do I run TypeScript directly?

We can use the ts-node package to execute TypeScript files from the command line. Install it with npm or other package manager. After that, simply execute the TypeScript files with the command: ts-node filename.

Can browsers execute TypeScript directly?

TypeScript cannot be run or understood in any browser. So, TypeScript is compiled to JavaScript (which browsers can understand). TypeScript can use all ECMAScript 2015 features and during the compilation they will be converted to target compile options like ES5.

How do I use TypeScript instead of JavaScript?

To run TypeScript in a browser, it needs to be transpiled into JavaScript with the TypeScript compiler (tsc). In this case, tsc creates a new . js file based on the . ts code, which you can use any way you could use a JavaScript file.


1 Answers

will run typescript without node example.js command How can I do it

You can use ts-node to compile + run directly (without ever writing to disk):

ts-node example.ts

More

https://github.com/TypeStrong/ts-node

like image 81
basarat Avatar answered Sep 27 '22 19:09

basarat