First TypeScript Program
Writing your first TypeScript hello world program is the best way to see how the language actually works. You will write a small file, run the tsc command to compile it, and then execute the generated JavaScript with Node. This hands-on loop teaches you the core workflow you will use in every future project.
This tutorial assumes you have already finished the TypeScript Installation & tsconfig.json setup. If not, complete that step first. Here, you will create hello.ts, add a typed function, see what happens when you pass the wrong type, and read the real compiler error. You will also learn common beginner errors and how to avoid them.
What Do You Need Before Writing Your First Program?
Before you begin, you need a working TypeScript project folder with a tsconfig.json file and the TypeScript compiler installed locally. You also need Node.js so you can run the compiled JavaScript. The TypeScript Installation & tsconfig.json tutorial covers all of this in detail.
If you have not set up a project yet, run these commands first. They create a folder, install TypeScript, and create a basic config:
mkdir my-ts-app
cd my-ts-app
npm init -y
npm install -D typescript
npx tsc --init
After npx tsc --init, open tsconfig.json and make sure strict is set to true. This tutorial uses TypeScript 5.x with strict mode on, so the error messages you see will match.
How Do You Write a TypeScript Hello World Program?
A TypeScript hello world program is a small file that prints a greeting to the terminal. It shows the full cycle: write TypeScript, compile it to JavaScript, and run the JavaScript. Start by creating a folder called src and a file inside it named hello.ts.
// hello.ts
const message: string = "Hello, world!";
console.log(message);
// Output: Hello, world!
This file uses a type annotation. The : string part tells TypeScript that message will only hold text. The console.log call prints the value to the terminal.
How Do You Compile TypeScript with the tsc Command?
Browsers and Node.js cannot run TypeScript files directly. You must compile TypeScript into JavaScript first. The tsc command is the compiler. It reads your .ts file, checks the types, and writes a .js file.
If your tsconfig.json has "outDir": "./dist" and "rootDir": "./src", run this from your project folder:
npx tsc
TypeScript looks at src/hello.ts and creates dist/hello.js. Open that file and you will see the compiled JavaScript:
// dist/hello.js (generated by tsc)
const message = "Hello, world!";
console.log(message);
// Output: Hello, world!
How it works: The tsc command reads tsconfig.json to know where your source files live and where to put the output. It strips out all type annotations because JavaScript does not understand them. The resulting .js file is plain JavaScript that any browser or Node.js can run. This conversion from .ts to .js is the heart of the compile-then-run workflow.
How Do You Run a TypeScript File After Compiling?
Once you have the compiled JavaScript, you can run TypeScript file output with Node.js. Point Node at the file inside dist:
node dist/hello.js
The terminal prints:
Hello, world!
This is the full loop: write hello.ts, compile with tsc, then run the .js output with Node. You will repeat this loop hundreds of times as you learn. Later, you can use npx tsc --watch to recompile automatically whenever you save a file.
How Do You Add a Typed Function to Your Program?
A function is a block of code that takes input, does some work, and returns a result. In TypeScript, you can declare the type of each input and the type of the return value. This is where TypeScript shines.
Update src/hello.ts to include a greet function:
// hello.ts
function greet(name: string): string {
return `Hello, ${name}! Welcome to TypeScript.`;
}
const message = greet("Riya");
console.log(message);
// Output: Hello, Riya! Welcome to TypeScript.
How it works: The greet function takes one parameter called name. The : string after name says the argument must be text. The : string after the closing parenthesis says the function will return text. When you call greet("Riya"), TypeScript checks that "Riya" is a string. It passes the check, so compilation succeeds.
The const message variable has no explicit type. TypeScript infers it from the return type of greet. Inference means TypeScript figures out the type automatically from context.
What Happens When You Pass the Wrong Type?
This is the moment TypeScript earns its keep. Suppose you call greet with a number instead of a string:
// hello.ts (broken version)
function greet(name: string): string {
return `Hello, ${name}!`;
}
const message = greet(42);
console.log(message);
// Error: Argument of type 'number' is not assignable to parameter of type 'string'.
Run npx tsc and the compiler stops with an error. It does not create the JavaScript file because the code is invalid. The exact message is:
src/hello.ts:5:23 - error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.
5 const message = greet(42);
~~
Found 1 error in src/hello.ts:5
How it works: TypeScript checks every function call against the function's declared parameter types. The greet function says name: string, so passing 42 (a number) violates the contract. The compiler refuses to build the project. This early error saves you from a runtime bug where the template string would print "Hello, 42!" and hide the real problem. Fix the call to greet("Riya") and the error disappears.
Compile-Then-Run vs Run Directly
There are two ways to work with TypeScript. The table below compares them so you can choose the right tool for each situation.
| Method | Command | Checks Types? | Best For |
|---|---|---|---|
| Compile then run | npx tsc then node dist/hello.js |
Yes, at compile time | Real projects and learning |
| Run directly with tsx | npx tsx src/hello.ts |
No, strips types only | Quick experiments |
| Watch mode | npx tsc --watch |
Yes, on every save | Active development |
The compile-then-run workflow is the safest for beginners because the compiler checks your types before you execute anything. Direct runners like tsx are faster for tiny tests but skip type checking, so a broken program may still run and hide bugs.
Common Mistakes and Best Practices
- Running
tscwithout a local install. If you see "command not found" fortsc, you likely tried to run it directly instead of throughnpx. Always usenpx tscso npm finds the local compiler in your project. - Passing a file to
tscand expectingtsconfig.jsonto apply. When you runnpx tsc hello.ts, TypeScript ignorestsconfig.jsoncompletely. To use your project settings, runnpx tscwith no file argument. - Forgetting to create the
srcfolder. IfrootDiris./srcbut nosrcfolder exists, the compiler complains. Create the folder and puthello.tsinside it before runningtsc. - Editing the generated
.jsfile. Any change you make todist/hello.jsis lost the next time you compile. Always edit the.tssource file instead. - Ignoring the compiler error and trying to run the old
.jsfile anyway. Iftscfails, the previous.jsfile may still exist, so Node may run stale code. Fix the error and recompile before testing.
Real-World Example: A Small Greeting Module
Here is a complete, realistic example that combines what you have learned. It splits the code into two files and shows how types flow across imports.
File: src/greet.ts
export function greet(name: string): string {
return `Hello, ${name}!`;
}
export function shout(message: string): string {
return message.toUpperCase();
}
File: src/hello.ts
import { greet, shout } from "./greet.js";
const user = "Arjun";
const greeting = greet(user);
const loud = shout(greeting);
console.log(greeting);
console.log(loud);
// Output:
// Hello, Arjun!
// HELLO, ARJUN!
Compile and run:
npx tsc
node dist/hello.js
How it works, line by line. The greet function in greet.ts accepts a string and returns a string. The shout function also accepts and returns a string. In hello.ts, the import path uses ./greet.js because the NodeNext module setting expects the final JavaScript extension. The variable user is a string, so greet(user) is valid. The result greeting is inferred as a string, so passing it to shout is also valid. Both console.log calls print to the terminal. When you run npx tsc, TypeScript checks both files, writes dist/greet.js and dist/hello.js, and then node dist/hello.js runs the compiled code. If you changed const user = "Arjun" to const user = 42, the compiler would stop with the same TS2345 error you saw earlier.
Summary
- Your first TypeScript hello world needs only a
.tsfile with a typed variable and aconsole.log. - Use the
tsccommand to compile TypeScript into JavaScript before running it with Node. - The
tsccommand readstsconfig.jsonto know where to find source files and where to write output. - Typed functions like
greet(name: string)catch wrong arguments at compile time, before users see a bug. - Passing the wrong type produces a real compiler error such as TS2345 and stops the build.
- Avoid common mistakes: always use
npx tsc, do not pass a file totscif you wanttsconfig.jsonto apply, and never edit the generated.jsfile.
Now that you can write, compile, and run a program, continue to the TypeScript Basic Types tutorial to learn the full set of types you can use in your code.
Frequently Asked Questions
Why does tsc say "command not found" on my machine?
This usually means TypeScript is not installed globally, or you typed tsc instead of npx tsc. Because we recommend a local install, the compiler lives inside your project's node_modules folder. Use npx tsc and npm will find it. If that fails, run npm install -D typescript again inside the project folder.
Can I run a .ts file directly without compiling it?
Yes, using a tool like tsx or ts-node. Run npx tsx src/hello.ts and the tool strips the types and executes the code in Node. However, these tools skip type checking, so a program with type errors may still run. For learning, compile with npx tsc first so you see real errors.
Why did tsc ignore my tsconfig.json settings?
When you pass a file name to tsc, such as npx tsc hello.ts, the compiler compiles only that file and skips tsconfig.json. To use your project settings like outDir and strict, run npx tsc with no file argument. The compiler then reads tsconfig.json and builds the whole project.
What does the error "Argument of type 'number' is not assignable to parameter of type 'string'" mean?
It means you called a function with the wrong type of value. The function expects a string, but you gave it a number. TypeScript stops the build to prevent a bug. Fix the call by passing a string, such as greet("Riya") instead of greet(42), and compile again.
Where does the compiled JavaScript file go?
It goes wherever outDir points in your tsconfig.json. If outDir is ./dist, then src/hello.ts becomes dist/hello.js. If you have not set outDir, the .js file appears next to the .ts file. Keeping output in a separate folder keeps your project clean.
Practice Exercises
Exercise 1 (Easy): Write and Run a Hello World File
Create src/hello.ts with a typed string variable and a console.log. Compile it with npx tsc and run the output with Node.
Show solution
const message: string = "Hello from TypeScript!";
console.log(message);
// Output: Hello from TypeScript!
npx tsc
node dist/hello.js
tsc compiles the file into dist/hello.js, and Node runs it to print the message.
Exercise 2 (Easy): Add a Typed Function
Add a function called greet that takes a name: string and returns a greeting. Call it with your own name and print the result.
Show solution
function greet(name: string): string {
return `Hello, ${name}!`;
}
console.log(greet("Meera"));
// Output: Hello, Meera!
The parameter and return type are both declared as string, so TypeScript checks the call and the returned value.
Exercise 3 (Medium): Trigger and Fix a Type Error
Call greet(42) and run npx tsc. Read the error message, then fix the call so the project compiles.
Show solution
function greet(name: string): string {
return `Hello, ${name}!`;
}
// greet(42);
// Error: Argument of type 'number' is not assignable to parameter of type 'string'.
console.log(greet("Kiran"));
// Output: Hello, Kiran!
The compiler rejects the number argument. Changing it to a string removes the TS2345 error and lets the build succeed.
Exercise 4 (Challenge): Two Files with an Import
Create src/greet.ts that exports a typed greet function. Create src/hello.ts that imports it and calls it. Compile and run both files.
Show solution
// src/greet.ts
export function greet(name: string): string {
return `Hello, ${name}!`;
}
// src/hello.ts
import { greet } from "./greet.js";
console.log(greet("Anaya"));
// Output: Hello, Anaya!
npx tsc
node dist/hello.js
With NodeNext modules, the import path uses .js, and tsc compiles both files into dist before Node runs hello.js.
Written by Shaikh Abdul Shahid
A Full-Stack Developer with experience in PHP, Laravel, JavaScript, and React.
Founder of Online Learner, sharing practical knowledge and programming tutorials.
Building and maintaining real-world web applications since 2017.
Your Feedback
Help us improve by sharing your thoughts.
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
