Skip to content
Related Articles

Related Articles

How to install TypeScript ?

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 24 Feb, 2022

Typescript validates your JavaScript ahead of time with static type checking. The code is interpreted by a browser, but if your code is broken you won’t catch it until runtime when the browser rows an error. The language is a strict superset of JavaScript, which means when you open up a ts file you can write plain JavaScript with all of its extra features being completely optional.

We can run the typescript compiler using the tsc command, it will take the typescript file and transpile it into vanilla JavaScript. A typescript project always has a ts config file, which helps us to customize the behavior of the compiler. The primary goal of typescript is to enable static typing, which is achieved by allowing the user to annotate code with types.

Prerequisite:

Following is the step-by-step guide to install TypeScript on:

Step 1: Go to the start menu and click on the command prompt.

Step 2: To install typescript we have to use NPM, NPM is called a node package manager. As its name suggests, it is used to install NodeJS packages onto our system.

Run the following command in the same NodeJS command prompt to install the typescript:

npm install --global typescript

The above command will install the typescript onto our local system. To verify the installation, run the following command:

tsc -v

Here tsc is a typescript compiler and the -v flag indicates that we are displaying the version of the typescript as shown in the image below:

At this point, typescript is successfully installed onto your system.

Step 3: To install a specific version of a typescript, use the following command with ‘@’ followed by version.

npm install --global typescript@4.x.x

Here, ‘x’ can be changed with the version that we want to install, as shown in the image below:

Step 4: To uninstall typescript, use the same command that is used to install the typescript and replace the installation with uninstall. Here is the command:

npm uninstall --global typescript

Now, let’s create a simple hello world project through typescript:

Step 1: Create a folder onto your desktop and create a file named “main.ts”. You can write whatever name you want for this file and folder.

Step 2: Open up the NodeJS command prompt and navigate to the folder path that we created in step 1.

cd path/to/the/folder

Step 3: Run the Initialization command in that command prompt to create the necessary configuration file for our simple project.

tsc --init

This tsc –init command will create a configuration file for the typescript compiler called tsconfig.json. This file contains all the configurations for the typescript compiler that we will be going to use while executing the program.

Step 4: Open up the main.ts that we created in the step1. Below is the hello world code in the typescript. It will display the string in the console that will confirm the working of the typescript compiler.

Typescript main.ts Code:

main.ts




/**  Hello world programm using Typescript  */
  
let helloTypescript: string;
    
helloTypescript = 
  "Typescript is successfully installed"+
  " onto this system. -GeeksforGeeks";
    
console.log(helloTypescript);


The above code should be pasted and saved in the main.ts file before executing the typescript compiler.

Step 5: Run the typescript compiler with the file name as an argument to see results.

tsc

This will create a compiled JS file from that typescript file. And now to run the JS file we will be using the NodeJS as below.

node main.js

Output:

Typescript hello world 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!