Languages
[Edit]
EN

TypeScript - how to compile ts file to js

17 points
Created by:
cory
316

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.

1. Single *.ts file compilation example

Simple steps:

  1. Create script.ts file:
    for (let i = 0; i < 5; ++i) {
        console.log('Hello world!');
    }
  2. 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.
  3. Run compiled program
    node Script.js

    Output:

    Hello world!
    Hello world!
    Hello world!
    Hello world!
    Hello world!

2. Multiple *.ts files to  single *.js file compilation example

Read this article for more details.

TypeScript

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join