How to Install and Setup TypeScript
In this tutorial, we will learn how to install or set up the TypeScript Environment for Microsoft Windows operating system. Basically, there are two ways to set up the Typescript environment, these are:
Installation of TypeScript using npm (node.js package manager)
Node.js is a JavaScript runtime environment that is used to run server-side applications written in JavaScript or TypeScript, while the NPM is a node package manager which is used to install the packages that are used in the node and web development.
The steps of installing the node and npm are given below:
Step 1: Downloading Node Js
Go to the official site of the node.js or click on the given link: https://nodejs.org/en/ (It will directly take you to the official website of the node.js)
Click on any of the green boxes, to download the node.js automatically to your system.
Step 2: Accepting policies
After downloading it, then we need to install it in our system. Click on the Run button.
Step 3: Selecting Features
Click on "Node.js runtime" and then click on the Next button.
Click on the Next button.
Step 4: Installing node.js
Click on the Install button.
Wait until the installation gets finished, and then click on the Finish button.
Step 5: Checking node.js version
To check the version of NodeJS, open Command Prompt and then write the command node -v
and then press enter.
node -v # checking node version
Step 6: Checking npm version
To check the npm version, write a command npm -v
on the command prompt and then press enter.
npm -v # Checking npm version
So, node and npm server is installed within your local system. Let's move forward and set up a typescript environment.
Step 7: Installing TypeScript Compiler
To install the TypeScript compiler globally, write the command npm install -g typescript
and then press enter.
# For installing TypeScript globally
npm install typescript -g
# Replace x.x.x with a Specific TypeScript Version
npm install -g typescript@x.x.x
# For installing TypeScript as dev dependency
npm install typescript --save-dev
# For installing latest TypeScript version
npm install typescript@latest -g
You can also install particular versions of Typescript or install Typescript as dev dependency.
Step 8: Checking TypeScript compiler version
To check the version of the TypeScript compiler installed in your system, write command tsc -v
and then press enter.
We can check the version of the TypeScript compiler installed in our system using the command tsc -v
and then press enter.
tsc -v
You can also install TypeScript in any specific IDE like VSCode, Eclipse, etc. For this purpose, you must install the TypeScript extension/plug-in in the respective IDE.
Creating First Program in TypeScript
We can use an IDE like Visual Studio Code, Eclipse, Atom, etc., to compile and run TypeScript code. In this post, we'll show you how to create a TypeScript program in VSCode; for this purpose, go through the following steps:
Step 1: Creating a New TypeScript File
First, create a new file with the .ts
extension, such as "example.ts" and write the TypeScript code in this file. For example, we want to print the message “Welcome to studytonight.com” on the console five times. For this purpose, we'll run the following code:
for(let i = 0; i < 5; i++)
{?
console.log("Welcome to studytonight.com");?
}
Step 2: Compiling the TypeScript Code
Now execute the following command to transpile the given TypeScript code to JavaScript Code:
tsc example.ts # transpiling the TypeScript code to JavaScript Code
When we execute the tsc example.ts
command, a JavaScript file with the same name and .js
extension is created.
Step 3: Printing the Output
Now run the given command to execute the newly created JavaScript file and get the desired results on the console:
node example.js. # executing the JavaScript Code
The output of the code will be:
As you can see in the image above the message "Welcome to studytonight.com" will be printed 5 times on the terminal.
Uninstalling TypeScript
If TypeScript is no longer needed and you want to free up some space, simply execute the following command:
npm uninstall typescript -g
Conclusion
TypeScript is an open-source, high-level programming language that can be installed by executing the command npm install typescript -g
. This command globally installs the default version of TypeScript on our system. Other than this, we can also install a specific TypeScript version by executing the command npm install -g typescript@x.x.x
; where "x.x.x" represents a TypeScript version that we want to install.