How to execute TypeScript file using command line?

 class=

TypeScript is an open-source programming language. It is developed and maintained by Microsoft. TypeScript follows JavaScript syntactically but adds more features to it. It is a superset of JavaScript. In order to execute or run any typescript file, first you need to install node and using it install typescript globally in your local system.

    Syntax:

  • To check node is installed, run command if not you have to installed it first:
    node -v
  • Now to install typescript, use:
    npm install -g typescript

After installing typescript, create a .ts file, for example, greet.ts as given below:

  • Example:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    var greet: string = "Greetings"
    var geeks: string = "GeeksforGeeks"
    console.log(greet + " from " + geeks); 
    // save the file as hello.ts 

    chevron_right

    
    

  • Output:
    Greetings from GeeksforGeeks

Procedure 1: This typescript file greet.ts will create a javascript file at runtime with the same name. To run any typescript file there are a few ways:
Syntax:



  • Step 1: First, run the typescript file with the following command. This will create a javascript file from typescript automatically with the same name.
    tsc helloWorld.ts
  • Step 2:Now run the javascript file, the greet.ts file will get executed:
    node helloWorld.js

Procedure 2: You can merge both the commands by using a pole | and && like below :
Syntax:

  • In Windows:
    tsc greet.ts | node greet.js
  • In Linux or MacOS:
    tsc helloWorld.ts && node helloWorld.js

Procedure 3: You can also install ts-node along with typescript using the following command:

Syntax:

  • To install:
    npm install -g ts-node
  • To run:
    ts-node helloWorld.ts

Output: Using any of the three ways, the output will remain the same.

Greetings from GeeksforGeeks

full-stack-img

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.