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! π

M.Sc. (Information Technology). I explain AI, AGI, Programming and future technologies in simple language. Founder of BoxOfLearn.com.