No history yet

Introduction to TypeScript

What is TypeScript?

JavaScript is the language of the web, but it has a quirk: it's dynamically typed. This means you don't have to declare the type of a variable. While flexible, this can lead to unexpected errors. Imagine you have a function that expects a number, but you accidentally pass it a string. JavaScript might not complain until the code runs, causing a bug that can be tricky to find.

TypeScript solves this problem. It’s a superset of JavaScript, which means any valid JavaScript code is also valid TypeScript code. It adds an optional layer of static typing on top.

TypeScript is a superset of JavaScript that adds static typing, classes, interfaces, and other features to the language.

Think of it like this: JavaScript is like building with LEGOs without instructions. You can build anything, but you might accidentally put a wheel where a window should go. TypeScript is like having a blueprint that tells you which pieces go where, preventing mistakes before you even start building.

The Power of Static Typing

The main benefit of TypeScript is static typing. This means the code is checked for type errors before it runs, during development. When you write code, your editor can instantly tell you if you're using a variable incorrectly. This saves a massive amount of time on debugging.

Lesson image

Let's see a simple example. In plain JavaScript, you could write this:

function add(a, b) {
  return a + b;
}

add(5, "10"); // Returns "510" - probably not what you wanted!

The + operator in JavaScript will concatenate a number and a string, leading to a logical error. TypeScript catches this immediately:

function add(a: number, b: number) {
  return a + b;
}

add(5, "10"); 
// Error: Argument of type 'string' is not assignable to parameter of type 'number'.

By adding : number after our parameters, we're telling TypeScript what kind of data to expect. This simple annotation prevents a common type of bug.

Core Features

Beyond basic type annotations, TypeScript offers several features that help organize and structure large applications. Let's look at three key ones: interfaces, classes, and modules.

interface

noun

A way to define the shape of an object. It's like a contract that an object must follow.

Interfaces are perfect for describing the structure of objects. For example, if you're working with user data, you can define an interface to ensure every user object has the properties you expect.

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

function greetUser(user: User) {
  console.log(`Hello, ${user.name}!`);
}

Classes in TypeScript work much like they do in other object-oriented languages, but with the added benefit of type safety for properties and methods.

class Animal {
  name: string;

  constructor(name: string) {
    this.name = name;
  }

  move(distance: number): void {
    console.log(`${this.name} moved ${distance} meters.`);
  }
}

Finally, Modules help you organize your code into separate files. Just like in modern JavaScript, you can export variables, functions, or classes from one file and import them into another. This keeps your codebase clean and manageable as it grows.

// In math.ts
export function add(a: number, b: number) {
  return a + b;
}

// In main.ts
import { add } from './math';

console.log(add(2, 3)); // 5

Setting Up Your Environment

To get started with TypeScript, you need two things: the TypeScript compiler and a code editor that supports it, like Visual Studio Code.

You can install TypeScript globally on your machine using npm (Node Package Manager). If you don't have Node.js installed, you'll need to do that first.

npm install -g typescript

Once installed, you can create a TypeScript file (with a .ts extension) and compile it into plain JavaScript using the TypeScript Compiler (tsc).

tsc your-file.ts

This command creates a new file, your-file.js, that can be run in any browser or Node.js environment. Typically, you'll create a tsconfig.json file in your project to configure the compiler's behavior, like specifying which JavaScript version to target.

Time to check your understanding.

Quiz Questions 1/5

What is the primary problem that TypeScript aims to solve in JavaScript development?

Quiz Questions 2/5

True or False: Any code that is valid in JavaScript is also valid in TypeScript.

TypeScript makes JavaScript more robust and predictable. By adding a type system, it helps you catch errors early, write cleaner code, and build applications that are easier to maintain and scale.