Table of contents
1.
Introduction
2.
Writing the First Program in TypeScript
3.
FAQs
4.
Key Takeaways
Last Updated: Mar 27, 2024

TypeScript First Program

Author Manvi Chaddha
0 upvote

Introduction

JavaScript is one of the most famous programming language and is the preferred choice for both frontend and backend. However there are some basic types of errors that a programmer usually faces, examples include simple typos, failure to understand the API surface of a library, some incorrect assumptions about the runtime behavior or other types of errors. 

(Source: Tenor)

The goal of using TypeScript is that it acts as a static type checker for your JavaScript programs, or in other words, it’s a simple tool that runs before your code runs and ensures that the types of the program are correct.

(Source: tenor)

Interesting ain’t it? Let’s start with writing our first Program, the famous Hello World Program in TypeScript

Writing the First Program in TypeScript

Before writing the program, it’s recommended that you install the latest stable version of Typescript using the command

npm install -g typescript
You can also try this code with Online Javascript Compiler
Run Code

This will install the latest stable version of TypeScript on your device. 
Let’s get started with the first program of TypeScript, Open up a text editor and create a new file and save it with the .ts extension.

Let us start with the traditional Hello World Example

var message:string = "Hello World"
console.log(message)

To Compile the TypeScript code,  Open up the command prompt or the integrated terminal and navigate to the directory where you have saved the code.

The above command on execution will generate a .js file at the same location where the TypeScript source file exists.  
The content of the .js file is

var message = "Hello World";
console.log(message);
You can also try this code with Online Javascript Compiler
Run Code

To run the program written, type the following in the terminal

node Hello.js
You can also try this code with Online Javascript Compiler
Run Code

The output will be

FAQs

  1. What is TypeScript? 
    TypeScript lets you write JavaScript the way you really want to. TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. TypeScript is pure object-oriented with classes, interfaces, and statically typed. 
     
  2. What is the extension of TypeScript files? 
    You use the extension .ts to save the TypeScript files.

Key Takeaways

In this blog, we wrote our first Program in TypeScript. We just started off our programming journey with TypeScript. If you want to learn more about web technologies, I recommend you to read our blogs on web technologies

Apart from that, you can refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, and System Design

Keep Learning and Keep Growing

Live masterclass