DEV Community

Cover image for My First Experience with TypeScript
Seema Saharan
Seema Saharan

Posted on

8 2

My First Experience with TypeScript

I was going to start a challenge by MLH: Local Hack Day, and the challenge was to write Hello World in a new programming language, and I thought I have never ever worked or even saw a code in TypeScript, so I started exploring and learning about it, and I literally got shocked and amazed how it works.
Don't judge, haha, I am a noob in TS.

Why I got amazed?

Since browsers natively does not understand typescript, but they understand javascript. So in order to run typescript codes, first it is transpiled to javascript.

And when I compiled the program, it created a new file with .js, as it should be. This was the most shocking, I really don't know why, because I have never seen such type of working in any other programming languages that I have used. It was cool.

I just started with TS, so created my first program.

What is TypeScript?

“TypeScript is JavaScript for application-scale development.”

TypeScript is an open-source programming language. It is developed by Microsoft.
It follows JavaScript syntactically but adds more features to it. It is a superset of JS.

TS and JS

Feature of TS

  • Purely object-oriented, with features like classes, objects, and interfaces like Java
  • TypeScript supports other JS libraries
  • Any valid .js file can be renamed to .ts and compiled with other TypeScript files
  • TypeScript is portable

Running a TS code

Browsers natively does not understand typescript, but they understand javascript. So in order to run typescript codes, first it is transpiled to javascript.

tsc : is a typescript compiler(transpiler) that converts typescript code into javascript.

You can install tsc by running the following command:

npm install -g typescript

Write your first program

  • Create a file named "helloworld.ts".
  • Add these lines of code.
var greet: string = "Greetings"; 
var message: string = "MLH Local Hack Day!"; 
console.log(greet + " from " + message); 
Enter fullscreen mode Exit fullscreen mode
  • To compile, run the following command:

tsc helloworld.ts

This command will generate a javascript file with name helloworld.js

  • Run the javascript file using the following command on the command line :

node helloworld.js

  • Output:

Greetings from MLH Local Hack Day!

GitHub repo for the same

Hit a ❤, if you are also new to TS.

You can connect with me on:
🎥YouTube
LinkedIn
🧵Twitter
Hashnode
📷Instagram

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (2)

Collapse
 
krishnakakade profile image
krishna kakade

Hi

Collapse
 
seemasaharan profile image
Seema Saharan

Hey

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more