Go beyond “Hello, World!” – The new battery

Last week, I published an introductory article on TypeScript (“TypeScript Tutorial: A User’s Guide to the Programming Language”) in which I not only introduced you to the language, but walked you through creating your first app. This application was the ubiquitous “Hello, World!” which does one thing – print the phrase “Hello, World!” in the airport.

It’s great… but it only serves to help you take your first steps with the language. Of course, TypeScript can do more than just print “Hello, World!” and I want to help you take these important next steps.

In case you haven’t already read the first article, be sure to do so as it won’t just walk you through the “Hello, World!” app, it also demonstrates how to install TypeScript on the Linux platform using Node.js and npm. So before you continue reading this, make sure you have TypeScript installed and read about “Hello, World!” app creation.

Done and done.

Let’s move on.

Expand the “Hello, World!” Application

And you thought you were done with that little app. Never. What we were going to do is highlight one of the cool features of TypeScript which allows it to print the message in a web browser. In the first part, we built the application that generated the hw.js file. If we run this application with the command node hw.jswe would see the correct output of Hello, new battery! (threw you for a loop there).

But what if we wanted to print this output in a web browser? We can do it and it’s quite simple. Let’s go through it.

First, go to the directory where you created the new project. From the original piece, this directory is called “helloworld”, so change to with:

cd ~/helloworld

You should see two files: hw.js and hw.ts. The hw.ts file is where we built the application and hw.js is the JavaScript file generated from the code, using the tsc hw.ts ordered.

Now we’ll create a basic HTML index file that calls the hw.js application and prints the output to a web browser. Since TypeScript is a superset of JavaScript, you might find this usage quite familiar (if you’ve worked with JavaScript before).

The first thing we are going to do is create an index.html file with the command:

nano index.html

We’ll add the usual HTML elements to this file to start with. So copy/paste the following (or create your own base index file) into the new file:

With the head section complete, let’s focus on the body. What we’re going to do is use the script-source call to point to the hw.js JavaScript file. This section looks like this:

Notice, in the section above I have the explicit path to the hw.js file. If you were to add ~/helloworld/hw.js, the app would have issues. For this reason, you want to make sure you type the full path to the JavaScript application.

Save and close the file.

Now open this file with your default web browser and it should display Hello, New Stack! (Figure 1).

Figure 1: Success! Our application was able to exit to the browser.

Now let’s go beyond “Hello, World!”.

Add numbers with TypeScript

Let’s use TypeScript to add two numbers. This will show not only how to use variables with the language, but also how to use the append function.

Create a new ts file with:

nano numbers.ts

The first line of the application will call the add function and look like this:

We end this line with a brace because we haven’t finished this section. You must then add:

The whole first section defines our function which will add the values ​​of x and y which we will define in the next line which looks like this:

The line above adds our x (3.14) to our y (10) but does nothing else. We then need to print the value we assigned to the sum variable with the line:

Our whole application looks like this:

You are on fire!

Save and close the file. Next, we’ll compile that into JavaScript with:

tsc numbers.ts

This will create the numbers.js file, which we can run with:

node numbers.js

The results of running the application will show 13.14 (because we added 3.14 to 10).

Alright, that’s great. What about a user’s input jack? This is also possible, using the parseInt function. Let’s create a simple application that asks the user for their name and prints it out. Before doing this, we need to install the type definitions for Node.js with:

npm i --save-dev @types/node

After taking care of that, create the new file with:

nano name.ts

In this file, paste the following:

In the code above, we:

  • Set const variable reading line to accept user input with the createInterface function.
  • Print the question “Who are you?” and assign the input to the variable Last name.
  • Print everything assigned to Last name.

Save and close the file. Compile the application in JavaScript with:

tsc name.tc

Now launch the application with:

node name.js

You will be asked for your name and the app will then print the name to the console.

And There you go! Your next steps with the TypeScript language. To dive deeper into this fantastic language, be sure to visit the official website TypeScript documentation.

Comments are closed.