2.TypeScript Installation, First Program, Compilation, and Execution Tutorial

2.TypeScript Installation, First Program, Compilation, and Execution Tutorial

AI Reading

Quick summary of this article

TypeScript is a programming language that builds on JavaScript by adding static typing. To get started, you first need to install TypeScript, which can be done either by downloading it from the official website or by using npm if you already have Node.js installed. After installation, you write your code in a .ts file, then compile it into a .js file using the TypeScript compiler, and finally run that JavaScript file with Node.js to see the output.

  • Install TypeScript globally via npm with the command: npm install -g typescript
  • Create a TypeScript file (e.g., first.ts) and write code using type annotations, such as var message: string = "Rajesh Kumar";
  • Compile the .ts file into a .js file by running: tsc first.ts
  • Execute the compiled JavaScript file using Node.js with: node first.js
  • The program will display the output: Rajesh Kumar

🛠️ Installation of TypeScript

📥 Step 1: Download TypeScript

To download the latest version of TypeScript, visit the official website:
👉 https://www.typescriptlang.org/

Alternatively, if you have Node.js installed, you can install TypeScript globally using npm:

npm install -g typescript

✍️ Step 2: Write Your First TypeScript Program

Create a new file named first.ts and add the following code:

var message: string = "Rajesh Kumar";
console.log(message);

🖥️ Step 3: Compile and Run the TypeScript Program

➡️ Open Visual Studio Code Terminal and compile the TypeScript file:

tsc first.ts

This will generate a first.js file in the same directory.


➡️ Run the compiled JavaScript file using Node.js:

node first.js

✅ You will see the output:
Rajesh Kumar

Leave a Reply

Your email address will not be published. Required fields are marked *