Welcome back! I am Mihir, and in this lesson we will write your first proper TypeScript program.
Instead of only printing "Hello World", we will build a small beginner-friendly example that uses variables, functions, object types, and compiler checking. This will show you why TypeScript is useful in real code.
The Goal
We will create a simple course enrollment program.
It will:
- Store student details
- Store course details
- Create a readable enrollment message
- Use TypeScript types to avoid mistakes
Create a File
Create a file named:
index.tsAdd this first line:
console.log("Welcome to TypeScript with Mihir!");Compile it:
npx tsc index.tsRun it:
node index.jsOutput:
Welcome to TypeScript with Mihir!This is the most basic TypeScript program. Now let us make it useful.
Add Typed Variables
const studentName: string = "Aarav";
const courseName: string = "TypeScript";
const durationInDays: number = 30;
const isActive: boolean = true;
console.log(studentName);
console.log(courseName);
console.log(durationInDays);
console.log(isActive);Here we are using four basic types:
| Value | Type |
|---|---|
"Aarav" | string |
"TypeScript" | string |
30 | number |
true | boolean |
TypeScript now knows what kind of value each variable should store.
If we write:
const durationInDays: number = "thirty";TypeScript warns us because "thirty" is a string, not a number.
Add a Function
Now let us create a function that builds a message.
function createEnrollmentMessage(
studentName: string,
courseName: string,
durationInDays: number
): string {
return `${studentName} enrolled in ${courseName}. Duration: ${durationInDays} days.`;
}
const message = createEnrollmentMessage("Aarav", "TypeScript", 30);
console.log(message);Let us understand the function:
studentName: stringThis means the first parameter must be a string.
durationInDays: numberThis means the third parameter must be a number.
): stringThis means the function must return a string.
If we return a number:
function createMessage(): string {
return 100;
}TypeScript warns us because the return type is wrong.
Add an Object Type
Passing many separate arguments can become confusing.
Instead, we can group related data in an object.
type Student = {
id: number;
name: string;
email: string;
isActive: boolean;
};This creates a custom type named Student.
Now create a student:
const student: Student = {
id: 1,
name: "Aarav",
email: "aarav@example.com",
isActive: true,
};If you miss a required property:
const student: Student = {
id: 1,
name: "Aarav",
email: "aarav@example.com",
};TypeScript warns that isActive is missing.
Add a Course Type
type Course = {
id: number;
title: string;
durationInDays: number;
level: "beginner" | "intermediate" | "advanced";
};The level field uses a union of string literals.
This means level can only be:
"beginner""intermediate""advanced"
Create a course:
const course: Course = {
id: 101,
title: "TypeScript",
durationInDays: 30,
level: "beginner",
};This would be invalid:
const course: Course = {
id: 101,
title: "TypeScript",
durationInDays: 30,
level: "easy",
};TypeScript warns because "easy" is not allowed.
Complete First Program
Here is the complete program:
type Student = {
id: number;
name: string;
email: string;
isActive: boolean;
};
type Course = {
id: number;
title: string;
durationInDays: number;
level: "beginner" | "intermediate" | "advanced";
};
function enrollStudent(student: Student, course: Course): string {
if (!student.isActive) {
return `${student.name} is not active and cannot enroll.`;
}
return `${student.name} enrolled in ${course.title}, a ${course.level} course for ${course.durationInDays} days.`;
}
const student: Student = {
id: 1,
name: "Aarav",
email: "aarav@example.com",
isActive: true,
};
const course: Course = {
id: 101,
title: "TypeScript",
durationInDays: 30,
level: "beginner",
};
const result = enrollStudent(student, course);
console.log(result);Output:
Aarav enrolled in TypeScript, a beginner course for 30 days.What TypeScript Checks Here
TypeScript checks:
student.idis a numberstudent.nameis a stringstudent.emailis a stringstudent.isActiveis a booleancourse.levelis one of the allowed valuesenrollStudentreceives aStudentand aCourseenrollStudentreturns a string
This gives us safety before the program runs.
Compile and Run
Compile:
npx tsc index.tsRun:
node index.jsIf there are no type errors, TypeScript creates JavaScript output.
If there are type errors, TypeScript shows warnings in the terminal and editor.
Important Beginner Tip
TypeScript errors are not your enemy.
They are feedback.
When TypeScript says something is wrong, it is usually telling you:
- A value has the wrong type
- A property is missing
- A function is being used incorrectly
- A value might be undefined
- Your data model is unclear
Learn to read TypeScript errors slowly. That skill will make you much stronger.
What You've Learned
You now know how to:
- Write a basic TypeScript file
- Use typed variables
- Create typed functions
- Define object types
- Use string literal unions
- Compile
.tsto.js - Run the compiled file with Node.js
- Understand how TypeScript catches mistakes
What's Next?
In the next lesson, we will learn tsconfig.json Basics.
This file controls how the TypeScript compiler behaves in your project.
Need Help?
- Have questions, confusion, or want to know more? Contact me
You have now written your first meaningful TypeScript program. From here, the language will start feeling more natural with practice.