Signup/Sign In
PUBLISHED ON: AUGUST 25, 2021

Hello World Program in TypeScript

We have already learned how to set up the environment for TypeScript. In this lesson, we will learn how to create our first TypeScript program and compile and run it.

Creating first TypeScript Program

To create a TypeScript program, you can use any code editor such as sublime code, VS code, etc.

In our case, we have used the Visual Studio code, then create a new file and save it with the .ts extension. You can provide the file name of your choice, but the extension must be .ts for the typescript program. Then write the code in that file.

Example: TypeScript Program

In the given example, we have created a basic TypeScript program.

let message = 'Welcome to Studytonight';
console.log(message);

Compiling TypeScript Program

To compile the TypeScript Program, open Command Prompt, and then go to the program file location.

To go for the file directory location, first, write cd command and then we can directly paste the location file location and then press Enter button.

prog_1

So, now we are in the same folder in which the program file exists.

Now write the compilation command that is tsc filename.ts. So, we have named our file hello.ts, then the compilation command is tsc hello.ts, and then press Enter button.

prog_2

If there is no error and the program is compiled successfully, then the TypeScript compiler creates a new file with the .js extension having the same code in the typescript file.

prog

To run the TypeScript code, write node filename.ts and then press Enter.

prog_3

Displaying output on the web browser

Apart from the command prompt, we can also display the output of the TypeScript program on the live server.

To display the output of the TypeScript code on the web browser, we have to first create an HTML file. Here, we have named our HTML file index.html you can provide the name of your choice.

<html>
    <head>
        <title>TypeScript Program</title>
    </head>
    <body></body>
</html>

Then embed the JavaScript file within the HTML file using the <script> tag.

<html>
    <head>
        <title>TypeScript Program</title>
    </head>
    <body>
        <script src="hello.js"></script>
    </body>
</html>

To run the given code on the web browser, we have to install the live server plugin.

Installing liver server plugin

To install the live server plugin, click on the extension button within the VS code and search for the live server.

prog_7

Select the first option and then click on the Install.

prog_8

After installing the live server, right-click the mouse, and you will get the option "Open with live server" on the dialog box.

Click on the open with live server option, then the live server will open the index.html file on the web browser.

prog

The live server will open the index.html file on the web browser with the URL: http://127.0.0.1:5500/index.html

prog

Output

The output of the following code is below. You can see this in the console tab of your browser.

prog

Conclusion

In this lesson, we have created a basic TypeScript program. Then we have compiled it and run on command prompt. Here, we have also explained how to display the output of the TypeScript command on the live server.



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.