Open In App

How to compile a Typescript file ?

Last Updated : 18 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

TypeScript is a robust, open-source programming language developed and maintained by Microsoft. As a syntactic superset of JavaScript, TypeScript enhances its foundational language by introducing additional features, including strong typing and object-oriented programming capabilities.

Unlike JavaScript, TypeScript cannot be executed directly in browsers. Instead, TypeScript files must first be compiled into JavaScript to function properly.

To compile and run TypeScript, you need to install Node.js and then use it to install TypeScript globally on your system. This guide will walk you through the process of compiling a TypeScript file.

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.



Next Article

Similar Reads