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?
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.
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.
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.
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
https://github.com/TypeStrong/ts-node
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With