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.ts
file:for (let i = 0; i < 5; ++i) { console.log('Hello world!'); }
- Run
tsc
command:tsc script.ts
or for automatic compilation when
script.ts
file is changed use:tsc -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
node Script.js
Output:
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.