Login Register
×

TypeScript Hello World Program

1️⃣ Create a New TypeScript File

First, you need a TypeScript file (.ts). If you haven’t already set up TypeScript, check out our TypeScript Installation and Setup Guide.

πŸ”Ή Step 1: Create a Project Folder (If Not Done Already)

If you don’t have a project folder yet, create one:

mkdir my-typescript-project
cd my-typescript-project

πŸ”Ή Step 2: Create a TypeScript File

Inside the project folder, create a new folder src and add a file named hello.ts:

mkdir src
cd src
touch hello.ts

Open hello.ts in a code editor (like VS Code) and write the following code:

let message: string = "Hello, World!";
console.log(message);

πŸ‘‰ Explanation:
βœ” let message: string = “Hello, World!”; β†’ Declares a variable message of type string.
βœ” console.log(message); β†’ Prints the message to the console.

This is your first TypeScript program! πŸŽ‰

2️⃣ Compile TypeScript Code to JavaScript

TypeScript does not run directly in browsers or Node.js. You need to compile it into JavaScript first.

πŸ”Ή Step 3: Compile TypeScript Code

Run the following command in the terminal (inside your project folder):

tsc src/hello.ts

This command converts hello.ts into hello.js inside the same directory (or the dist folder if configured).

3️⃣ Run the Compiled JavaScript Code

Now, run the JavaScript file using Node.js:

node src/hello.js

βœ” Output:

Hello, World!

πŸŽ‰ Congratulations! Your first TypeScript program is running successfully.

4️⃣ Using tsconfig.json for Better Project Structure

Instead of compiling each .ts file manually, you can configure TypeScript to automatically compile all files.

πŸ”Ή Step 4: Initialize a TypeScript Project

Run:

tsc --init

This creates a tsconfig.json file. Modify it to include:

{
"compilerOptions": {
"outDir": "./dist", // Stores compiled JavaScript files in 'dist' folder
"rootDir": "./src", // Stores TypeScript files in 'src' folder
"target": "ES6", // Converts TypeScript to ES6 JavaScript
"strict": true // Enables strict type checking
}
}

Now, you can compile the entire project by simply running:

tsc

βœ” This compiles all .ts files in src/ and saves the JavaScript files in dist/.

πŸ‘‰ Run the compiled file:

node dist/hello.js

🎯 This method keeps your project structured and easy to manage.

5️⃣ Using TypeScript with Live Compilation (Optional)

If you want TypeScript to automatically compile whenever you save changes, use ts-node.

πŸ”Ή Step 5: Install ts-node (Optional)

Run:

npm install -g ts-node

Now, you can directly run TypeScript files without compiling:

ts-node src/hello.ts

βœ” Output:

Hello, World!

πŸ‘‰ This method is faster for development, but for production, always compile .ts files to .js.

6️⃣ Why Writing a TypeScript “Hello World” Program is Important?

βœ… Understanding Basics – Helps in learning TypeScript syntax.
βœ… Project Setup – Teaches how to set up and configure TypeScript.
βœ… Compilation Process – Shows how TypeScript converts to JavaScript.
βœ… Execution – Helps in running TypeScript programs efficiently.

Now, you’re ready to write more complex TypeScript programs! πŸš€