TypeScript Program to Print Hello World: A Beginner’s Guide

Recently, I was introducing a colleague to TypeScript, and the first thing we did was create a simple “Hello World” program.

It’s always best to start with the basics when learning a new programming language.

In this article, I will explain different ways to create and run a “Hello World” program in TypeScript, from setting up your environment to executing your first TypeScript code. So let’s dive in!

What is TypeScript?

TypeScript is a superset of JavaScript that adds static typing to the language. It was developed by Microsoft to address some of the shortcomings of JavaScript, especially when building large-scale applications.

The code you write in TypeScript gets compiled into plain JavaScript, which can run in any browser or JavaScript environment.

Method 1: Setting Up TypeScript and Running a Hello World Program

Before we can write our first TypeScript program, we need to set up our development environment. Here’s how to do it:

Step 1: Install Node.js and npm

First, you need to have Node.js installed on your computer. This will also install npm (Node Package Manager), which we’ll use to install TypeScript.

To check if you have Node.js installed, open your terminal or command prompt and run:

node -v
npm -v

If you see version numbers, you’re all set. If not, download and install Node.js from nodejs.org.

TypeScript Program to Print Hello World

Step 2: Install TypeScript

Now, let’s install TypeScript globally on your machine:

npm install -g typescript

To verify the installation, run:

tsc --version

You should see the TypeScript version number.

Setting Up TypeScript and Running Program

Step 3: Create Your First TypeScript File

Create a new file named hello.ts and open it in your favorite code editor. Then add the following code:

// hello.ts
console.log("Hello, World!");

Step 4: Compile and Run Your TypeScript Code

Now, let’s compile our TypeScript code into JavaScript:

tsc hello.ts

This will create a hello.js file. Now run it using Node.js:

TypeScript Program to Print Hello World
node hello.js

You should see “Hello, World!” printed in your terminal.

Run TypeScript Code

Method 2: Using TypeScript Playground

If you don’t want to set up TypeScript locally, you can use the TypeScript Playground, which is an online editor provided by Microsoft.

Here’s how to use it:

  1. Go to TypeScript Playground
  2. Clear any existing code
  3. Type console.log(“Hello, World!”);
  4. Click the “Run” button

You’ll see “Hello, World!” in the output panel on the right.

Use TypeScript Playground to execute TypeScript Code

This is a great way to experiment with TypeScript without needing to install any additional software.

Method 3: Creating a TypeScript Project with npm

For a more professional setup, let’s create a proper TypeScript project:

Step 1: Create a New Project Directory

mkdir hello-typescript
cd hello-typescript

Step 2: Initialize a Node.js Project

npm init -y

This creates a package.json file with default settings.

Step 3: Install TypeScript Locally

npm install typescript --save-dev

Step 4: Create a TypeScript Configuration File

Create a file named tsconfig.json:

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "outDir": "./dist",
    "strict": true,
    "esModuleInterop": true
  },
  "include": ["src/**/*"]
}

Step 5: Create Your Source Directory and TypeScript File

mkdir src

Create a file named src/index.ts:

// src/index.ts
function greet(name: string): string {
  return `Hello, ${name}!`;
}

console.log(greet("World"));

Step 6: Add npm Scripts

Update your package.json to include these scripts:

"scripts": {
  "build": "tsc",
  "start": "node dist/index.js"
}

Step 7: Build and Run Your Project

npm run build
npm start

You should see “Hello, World!” in your terminal.

Method 4: Using TypeScript with VS Code

Visual Studio Code has excellent TypeScript support out of the box. Here’s how to use it:

  1. Install VS Code from code.visualstudio.com
  2. Create a new file named hello.ts
  3. Add the code: console.log(“Hello, World!”);
  4. Open the integrated terminal (Ctrl+` or View > Terminal)
  5. Compile and run the code:
tsc hello.ts
node hello.js

VS Code also supports TypeScript compilation on save. To enable this:

  1. Create a tsconfig.json file
  2. Add the following configuration:
{
  "compilerOptions": {
    "target": "es6",
    "outDir": "./dist",
    "watch": true
  }
}
  1. Run tsc -w in the terminal to start the watch mode

Now, every time you save your TypeScript file, it will automatically be compiled to JavaScript.

Going Beyond Hello World: Adding Types

Let’s enhance our Hello World program by adding some TypeScript-specific features:

// hello_enhanced.ts

// Defining a typed function
function greet(name: string, age?: number): string {
  if (age !== undefined) {
    return `Hello, ${name}! You are ${age} years old.`;
  }
  return `Hello, ${name}!`;
}

// Using an interface
interface Person {
  firstName: string;
  lastName: string;
  age?: number;
}

// Using the interface
const user: Person = {
  firstName: "John",
  lastName: "Doe",
  age: 30
};

// String interpolation with types
console.log(greet(`${user.firstName} ${user.lastName}`, user.age));

// Using an array with type
const cities: string[] = ["New York", "Los Angeles", "Chicago", "Houston"];
console.log(`Popular cities in the USA: ${cities.join(", ")}`);

Compile and run this code:

tsc hello_enhanced.ts
node hello_enhanced.js

You’ll see more advanced output using TypeScript’s type system.

How to create and Run TypeScript Code

Conclusion

I hope you found this article helpful. TypeScript is a powerful language that builds on JavaScript’s flexibility while adding the safety of static types. The “Hello World” program is just the beginning.

From here, you can explore interfaces, generics, decorators, and many other features that make TypeScript great for building robust applications.

Other TypeScript articles you may also like:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.