EN
TypeScript - how to compile ts file to js
17
points
In this short article, we would like to show how to compile *.ts files to *.js files.
To complile TypeScript file to JavaScript file, tsc program is necessary. In the below, you will find single output and multiple output compilation descriptions.
Single *.ts file compilation example
Simple steps:
- Create
script.tsfile:for (let i = 0; i < 5; ++i) { console.log('Hello world!'); } - Run
tsccommand:tsc script.tsor for automatic compilation when
script.tsfile is changed use:tsc -w Script.tsNotes:
- after compliation
script.jsfile should be created, -wparameter enables monitoring and recompilation when file is changed - files watching mode.
- after compliation
- Run compiled program
node Script.jsOutput:
Hello world! Hello world! Hello world! Hello world! Hello world!
Multiple *.ts files to single *.js file compilation example
Read this article for more details.