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.
Simple steps:
- Create
script.ts
file:xxxxxxxxxx
1for (let i = 0; i < 5; ++i) {
2console.log('Hello world!');
3}
- Run
tsc
command:xxxxxxxxxx
1tsc script.ts
or for automatic compilation when
script.ts
file is changed use:xxxxxxxxxx
1tsc -w Script.ts
Notes:
- after compliation
script.js
file should be created, -w
parameter enables monitoring and recompilation when file is changed - files watching mode.
- after compliation
- Run compiled program
xxxxxxxxxx
1node Script.js
Output:
xxxxxxxxxx
1Hello world!
2Hello world!
3Hello world!
4Hello world!
5Hello world!
Read this article for more details.