Open In App

How to compile a Typescript file ?

Last Updated : 30 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

TypeScript is an open-source programming language. It is developed and maintained by Microsoft. TypeScript follows JavaScript syntactically but adds more features to it. Typescript is a superset of javascript but it is a strongly typed, object-oriented programming language. Typescript file does not run directly on the browser as javascript run, we have to compile the typescript file to the javascript then it will work as usual. In order to compile & run any typescript file, first, we need to install node and using it install typescript globally in your local system. In this article, we will see how we can compile a Typescript file. Please refer to the How to execute TypeScript file using command line? article for the detailed installation process of node & typescript in the local system.

Setup for the Typescript: We need to download & install the node.js & node package manager (npm) to set up the environment for the typescript to work. In order to check the version installed for node & npm, type the below command in cmd:

node -v
npm -v

Note: We need not download the npm explicitly, it will automatically download & install along with the node.js.

After successful installation of node.js & npm, we need to install the Typescript using the below command:

npm install -g typescript

At this stage, we have successfully completed the environment setup for the Typescript to start work on it. Please note that we have to create & keep the typescript file in the same directory where we have installed the typescript. Save the typescript file with the extension of .ts & add the below code in your ts file.

Typescript:

Javascript




var greet : string = "Welcome to";
var orgName : string = "GeeksforGeeks!";
console.log(greet+ " "+orgName);


Now, run the following command in the command prompt to compile the typescript file. This will create a javascript file from typescript automatically with the same name.

Compiling typescript file:

tsc fileName.ts

Run the javascript file:

node fileName.js

After successful execution of the above command, a javascript file will be automatically generated in the current working directory and it will contain the compiled code of the typescript file. The generated javascript code will look like the following.

 

Example:

Javascript




var greet = "Welcome to";
var orgName = "GeeksforGeeks!";
console.log(greet + " " + orgName);


Output: 

"Welcome to GeeksforGeeks!"

Example: In this example, you will see the complete steps for the installation, configuring the directory, compiling & executing the ts code, that we have followed.

  • Create a folder and name it Typescript.
  • Open the command prompt and go to the Typescript directory and run the following command.
npm install -g typescript

Create a file index.ts and add the following code. We can follow either of the syntaxes to write the code in Typescript.

Typescript:

Javascript




const character = "GeeksForGeeks";
console.log(character);


Now we have to compile our typescript file to javascript, therefore run the following command.

tsc index.ts

Output: Now, the typescript file is compiled and we can have the index.js file in our directory, so we will check out the output by typing the below command.

node index.js

Output: From the above image, we can see that the javascript file is successfully running and show the respective output.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads