TypeScript Introduction
Welcome to the first tutorial in our TypeScript course. If you already know a little JavaScript, you are in the right place. TypeScript is built on top of JavaScript, and it is designed to make your life as a developer easier and safer.
In this lesson, we will answer three simple questions: What is TypeScript? Why should you learn it? And how does it actually work?
What is TypeScript?
TypeScript is a programming language that adds an extra layer on top of JavaScript. That extra layer is called a type system.
To understand this, let us quickly define JavaScript. JavaScript is the language that runs in your web browser. It makes websites interactive. When you click a button and something happens, that is usually JavaScript.
Now, JavaScript is a dynamically typed language. This means JavaScript does not strictly check the kind of data you put into a variable. You can put a number in a variable, and later put some text in the same variable. JavaScript will not stop you.
TypeScript is different. It is a statically typed language. This means TypeScript checks your code before it runs to make sure you are using the right kinds of data in the right places. For example, if you tell TypeScript that a variable should hold a number, it will give you an error if you try to put text inside it later.
The most important thing to remember is this: TypeScript is a superset of JavaScript. This is a fancy way of saying that any valid JavaScript code is also valid TypeScript code. You do not have to throw away your JavaScript knowledge. You just add a few new skills on top of it.
Why Does TypeScript Exist?
You might wonder, "If JavaScript works fine, why do we need TypeScript?"
Imagine you are building a small website with ten lines of code. JavaScript is perfect for that. But imagine you are working on a big project with thousands of lines of code, and you are working with a team of other developers. It becomes very easy to make mistakes.
For example, you might write a function that expects a number, but a teammate accidentally passes it a piece of text. In JavaScript, this bug might only show up when a user clicks a button on your live website. That is stressful.
TypeScript solves this by catching these mistakes while you are typing in your code editor. It acts like a helpful assistant that says, "Hey, you told me this was a number, but you just gave me text. Did you make a mistake?" This saves you hours of debugging and prevents many bugs from ever reaching your users.
How Does TypeScript Work?
TypeScript cannot run directly in a web browser. Browsers only understand JavaScript. So, how do we use TypeScript?
We use a tool called a compiler. The TypeScript compiler takes your TypeScript code (which has all those helpful type checks) and translates it into plain JavaScript. The browser then runs that plain JavaScript.
Think of it like this: You write a letter in a special language that only you and your assistant (the compiler) understand. Your assistant then translates that letter into English (JavaScript) so that everyone else (the browser) can read it.
Because the types are only used for checking during development, they disappear completely in the final JavaScript output. This means TypeScript does not slow down your website when users are actually using it.
A Quick Look at TypeScript Code
Let us look at a tiny example to see the difference.
Here is regular JavaScript:
let message = "Hello, world";
Here is the same code in TypeScript with an explicit type:
let message: string = "Hello, world";
That : string part is the type annotation. It tells TypeScript, "This variable message will only ever hold text (a string)." If you later try to write message = 42;, TypeScript will warn you that you made a mistake.
You might notice that TypeScript looks almost exactly like JavaScript. That is the beauty of it. You can start by writing normal JavaScript and slowly add type annotations only where you need them.
What You Will Learn in This Course
This course is designed for beginners. You do not need any prior TypeScript experience. However, you should know the basics of JavaScript (variables, functions, and objects) to get the most out of these lessons.
In the next tutorial, we will set up your computer so you can start writing and compiling TypeScript code. We will install the compiler and write our very first TypeScript program.
Summary of key terms:
- Type System: A set of rules that checks the kinds of data used in your code.
- Static Typing: Checking types before the code runs (at compile time).
- Compiler: A tool that converts TypeScript code into JavaScript code.
- Superset: A language that includes all the features of another language, plus extra features.
Ready to write some code? Let us move on to installing TypeScript.
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.
