GETTING STARTED WITH TYPESCRIPT

Mastering basics is the key to become successful in any technology. We’ve already seen What is TypeScript, What are the advantages and features of it, in our previous article. In this article we will be seeing how to get started with TypeScript, how to create program using the same. So let’s get started!!

In this post we will understand how TypeScript file compiles and run, We will be creating sample program for the same.

Step 1: Open any Editor, In my case I will be using Visual Studio Code, you can download the same from here if you don’t have.

Step 2: Open the Editor, Create new file and name it Sample.ts.

Step 3: Now in the same file we will be writing basic code, we will be starting with “Hello Nuclear Geeks” program.

Step 4: Write the below code in your Sample.ts file and save it.

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

We’re done with our sample program, Before we run the same let’s try to understand what we did, We’ve defined a variable name message of type string and assigned the message “Hello Nuclear Geeks”. Later we are printing the same message.

Step 5: Before we run our program, Let’s install Node.js: Node.js is an open source, cross-platform runtime environment for server-side JavaScript, Node.js is required to run JavaScript without a browser support. You can download Node.js from here https://nodejs.org/en/download/

Once you’re done with the Node.js download check the installation by typing

$ node --version 
v10.16.0$ 

Step 6: So now we’re done with the setup, Let’s run the program, Open the terminal and type

$ tsc Sample.ts

Step 7: Open the location of Sample.ts file, you will see Sample.js file getting created in the same location, it is due the fact that, when we call TypeScript compiler “tsc” and asks to run the file Sample.ts, it will internally create one equivalent JavaScript file and you can see how it converts every line of typescript into javascript code.

Step 8: Now let’s run the javascript file by typing below command,

$ node Sample.js 
Hello Nuclear Geeks 

And, here you see your output!

So, In this post we’ve seen how one can get started with TypeScript and start building sample programs and application. Do let me know in the comment section if you’ve any queries.

One thought on “GETTING STARTED WITH TYPESCRIPT

Leave a comment