No history yet

Introduction to TypeScript

JavaScript with Superpowers

Imagine writing JavaScript, but with a helpful assistant looking over your shoulder, catching typos and mistakes before you even run your code. That's essentially what TypeScript is: a superset of JavaScript that adds static types. All valid JavaScript code is already valid TypeScript, but TypeScript adds an extra layer of safety.

The main benefit is catching errors early. Instead of finding a bug in your application when a user clicks a button, TypeScript can spot the potential problem while you're still writing the code.

Let's look at a simple example. Here's some plain JavaScript:

function greet(person) {
  return "Hello, " + person.name;
}

let user = { name: "Alice" };
greet(user);

This works fine. But what if we accidentally pass an object without a name property? Or a simple string?

greet({ firstName: "Bob" }); // Returns "Hello, undefined" greet("Charlie"); // Error at runtime

TypeScript helps prevent these kinds of errors.

Understanding Types

TypeScript's power comes from its type system. You can explicitly tell TypeScript what kind of data to expect using type annotations.

let name: string = "Alice";
let age: number = 30;
let isStudent: boolean = true;

If you try to assign a different type of value to these variables, the TypeScript compiler will give you an error right away.

But you don't always have to write the types. TypeScript is smart enough to figure them out on its own through a process called type inference.

// TypeScript knows 'count' is a number without you saying so.
let count = 100;

// This would cause an error:
// count = "one hundred";

For more complex data, like objects, we can define a shape using an interface. An interface is like a blueprint for an object, ensuring it has the properties we expect.

interface User {
  name: string;
  id: number;
}

function greet(user: User) {
  return `Hello, ${user.name}`;
}

Now, if we try to call greet with an object that doesn't match the User interface, TypeScript will warn us before we run the code.

Interfaces in TypeScript allow you to define the shape of objects and specify the types of their properties.

Setting Up Your Project

Browsers can't run TypeScript directly. It needs to be compiled into standard JavaScript first. The setup process is straightforward.

First, you'll need Node.js and npm (Node Package Manager) installed on your system. Then, you can install the TypeScript compiler globally using this command in your terminal:

npm install -g typescript

Next, you can create a TypeScript file, which uses the .ts extension (for example, app.ts). To compile it, you run the tsc command.

tsc app.ts

This command creates a new file, app.js, which is the plain JavaScript version of your code. For example, if your app.ts file contains this:

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

The compiler will generate an app.js file that looks like this:

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

Notice how the type annotation : string is gone. The final output is clean JavaScript that any browser can understand. For larger projects, you'll typically create a tsconfig.json file to manage all your compiler options in one place.

Quiz Questions 1/5

What is the primary relationship between TypeScript and JavaScript?

Quiz Questions 2/5

Why does TypeScript code need to be compiled before it can run in a web browser?

By adding a simple type system on top of JavaScript, TypeScript helps you write more reliable code and catch errors before they reach your users.