No history yet

TypeScript Basics

JavaScript with Superpowers

JavaScript is flexible. Sometimes, it's too flexible. You might write a function expecting a number, but accidentally pass it a string. The code might not break immediately, but it can cause strange bugs later that are difficult to track down. This is where TypeScript comes in.

TypeScript is a superset of JavaScript, which means any valid JavaScript code is also valid TypeScript code. It adds one crucial feature on top: a type system. Think of it as a helpful co-pilot that checks your work as you write, not after you've already shipped it.

The only job of TypeScript is static checking—a fancy way of saying it catches errors while you're typing, before your code even runs.

This process is called static typing. It lets you define what kind of data your variables, function parameters, and function return values should hold. If you try to use the wrong type, TypeScript will alert you immediately. This simple check saves countless hours of debugging.

Getting Started

To use TypeScript, you first need to install its compiler. The compiler, called tsc, is a tool that reads your TypeScript code (usually in .ts files), checks it for type errors, and then converts it into plain JavaScript that can run in any browser. You can install it globally on your computer using npm (Node Package Manager), which comes with Node.js.

# Installs the TypeScript compiler globally
npm install -g typescript

Once installed, you can create a file named app.ts and write some TypeScript. To compile it, you run the tsc command in your terminal. This will generate a corresponding app.js file.

# Compiles app.ts into app.js
tsc app.ts

This generated JavaScript file is what you would include in your HTML, just like any other JS file. Your development workflow involves writing .ts files and compiling them to .js files before running them in the browser.

Defining Types

The core of TypeScript is adding type annotations. This is done using a colon after a variable or parameter name, followed by the type. Let's look at some basic types.

let username: string = "Alice";
let age: number = 30;
let isLoggedIn: boolean = true;

// This will cause a compile-time error!
age = "thirty";

You can also add types to function parameters and specify the type of the value the function returns.

function greet(name: string): string {
  return `Hello, ${name}`;
}

// TypeScript will catch this error:
greet(123);

But what about more complex data, like objects? For that, we use interfaces.

An interface is a way to define a custom type, like a blueprint for an object. It describes the shape of an object, specifying the property names and their corresponding types.

Imagine you're working with user data. Each user object should have a specific structure.

// Define a blueprint for a User object
interface User {
  id: number;
  name: string;
  isAdmin: boolean;
}

// Create a user that follows the blueprint
const currentUser: User = {
  id: 1,
  name: "Bob",
  isAdmin: false
};

// TypeScript will flag this object because 'name' is missing.
const invalidUser: User = {
  id: 2,
  isAdmin: true
};

By using an interface, you ensure that every User object in your application has a consistent shape. This makes your code more predictable and easier for other developers (and your future self) to understand.

Lesson image

This structured approach makes codebases easier to navigate and maintain, especially as projects grow in size and complexity.

Ready to check your understanding?

Quiz Questions 1/5

What is the primary benefit of using TypeScript over plain JavaScript?

Quiz Questions 2/5

The TypeScript compiler (tsc) converts TypeScript (.ts) files into plain JavaScript (.js) files that browsers can understand.

By adding a layer of type safety, TypeScript empowers you to build more robust and reliable applications. It's a small change in how you write code that pays huge dividends in quality and maintainability.