No history yet

Introduction to TypeScript

What is TypeScript?

Think of JavaScript as a free-flowing conversation. It's flexible and lets you say almost anything. TypeScript is like adding a grammar checker to that conversation. It doesn't change what you're saying, but it makes sure you're saying it correctly before you even speak.

Technically, TypeScript is a superset of JavaScript, which means any valid JavaScript code is also valid TypeScript code. The key difference is that TypeScript adds a layer of static typing. This simply means you can define what kind of data a variable should hold, like a number, a string, or a boolean.

If JavaScript is a versatile scripting language, TypeScript is that same language with added superpowers for building large, robust applications.

Why Bother with Types?

You might wonder why you'd want to add these extra rules. The main reason is to catch errors early. With regular JavaScript, you might not discover a type-related bug until you run the code, or worse, until your users do. TypeScript spots these errors as you type.

Lesson image

This leads to several key benefits:

  • Fewer Bugs: By catching type errors during development, you prevent them from ever making it into your final product.
  • Better Tooling: Code editors like VS Code can understand your TypeScript code, offering smarter autocompletion, navigation, and automated refactoring.
  • Improved Readability: Explicit types make your code easier for you and others to understand. When you see function calculatePrice(price: number), you know exactly what kind of data the function expects.

TypeScript is a powerful tool that enhances your JavaScript code by adding static typing, which helps catch errors early, improves code readability, and makes your projects more scalable.

Setting Up Your Environment

To get started with TypeScript, you'll need Node.js and its package manager, npm, which usually comes with it. If you have those installed, you can install TypeScript globally on your machine using a single command in your terminal.

npm install -g typescript

Once installed, you can check the version to confirm it's ready to go.

tsc --version

For any real project, you'll want a configuration file named tsconfig.json. This file tells the TypeScript compiler how to handle your project files. You can create a basic one with the following command:

tsc --init

This command generates a tsconfig.json file with many options, most of which are commented out. For now, the default settings are perfect for getting started.

Compiling to JavaScript

Browsers don't understand TypeScript directly. They only understand JavaScript. This is where the TypeScript compiler, tsc, comes in. It takes your TypeScript code and translates—or transpiles—it into plain JavaScript that any browser can run.

Let's say you have a file named main.ts with the following code:

// main.ts
let message: string = "Hello, TypeScript!";
console.log(message);

To compile this, you'd run the following command in your terminal:

tsc main.ts

This command creates a new file, main.js, in the same directory. The contents of that file will look very familiar:

// main.js
var message = "Hello, TypeScript!";
console.log(message);

Notice how the type annotation : string is gone. The compiler used it to check for errors and then produced clean, standard JavaScript. You can now use this main.js file in your HTML just like any other JavaScript file.

Let's test your understanding of these basics.

Quiz Questions 1/5

What is the primary relationship between TypeScript and JavaScript?

Quiz Questions 2/5

What is the main advantage of using TypeScript's static typing system?

That's the core idea of TypeScript. It's a tool that sits on top of JavaScript to help you write better, more reliable code, especially as your projects grow in size and complexity.