Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is TypeScript?

Welcome to the world of programming! If you're just starting out, you might have heard of TypeScript, but what exactly is it? In this blog, we'll explore what TypeScript is, why it's useful, and how you can start using it in your programming journey. We'll make sure to explain any jargon we use, and provide plenty of code examples and analogies to help you understand the concepts.

What is TypeScript?

TypeScript is a programming language that is a superset of JavaScript. But what does that mean? Think of JavaScript as a circle, and TypeScript as a larger circle that surrounds it. TypeScript contains all the features of JavaScript, and also adds some additional features that make it more powerful and easier to work with.

TypeScript was created by Microsoft in 2012 as a response to some of the challenges that developers faced when working with large JavaScript codebases. The main difference between TypeScript and JavaScript is that TypeScript is a statically typed language, while JavaScript is a dynamically typed language. Don't worry if you're unfamiliar with these terms – we'll explain them in more detail later.

TypeScript is not a completely separate language from JavaScript. In fact, TypeScript code is eventually compiled (or "transpiled") into plain JavaScript so that it can run in web browsers, just like JavaScript. This means that learning TypeScript also helps improve your JavaScript skills.

Let's dive into the main features of TypeScript and see how they can be helpful in your programming journey.

Static Typing

As mentioned earlier, TypeScript is a statically typed language, while JavaScript is a dynamically typed language. But what does that mean?

In a dynamically typed language like JavaScript, you don't have to declare the type of a variable when you create it. The type is determined at runtime, which means that the type of a variable can change as your program runs. For example, you can assign a string value to a variable, and later assign a number to the same variable:

let myVariable = 'Hello, world!';
myVariable = 42;

This flexibility can be helpful, but it can also lead to errors and make your code harder to understand.

In a statically typed language like TypeScript, you declare the type of a variable when you create it, and the type cannot change. This can help catch errors early, before your code even runs, and makes your code more predictable and easier to understand.

Here's the same example in TypeScript:

let myVariable: string = 'Hello, world!';
// This would cause an error in TypeScript, because myVariable is a string
myVariable = 42;

By declaring the type of myVariable as string, we prevent any unintended changes to its type later in the code.

Type Inference

You might be worried that declaring types for all your variables will make your code longer and more difficult to write, but TypeScript has a feature called type inference that makes this process easier. Type inference means that TypeScript can often figure out the type of a variable automatically, based on the value you assign to it.

For example, if you write the following code in TypeScript:

let myNumber = 42;

TypeScript will automatically infer that myNumber is of type number, so you don't have to explicitly declare the type. You can still explicitly declare the type if you want to, but in many cases TypeScript can save you some typing (pun intended).

Interfaces and Classes

TypeScript also adds support for interfaces and classes, which are features that help you write more organized and reusable code. These features are part of an approach to programming called object-oriented programming (OOP).

Interfaces

An interface is a way to define a "contract" for an object, specifying what properties and methods the object should have. For example, let's say you're writing a program to manage a list of books. You might define an interface for a Book like this:

interface Book {
  title: string;
  author: string;
  publicationDate: Date;
  isAvailable: boolean;
}

Now, you can use this interface to create objects that follow the same structure:

let myBook: Book = {
  title: 'The Catcher in the Rye',
  author: 'J.D. Salinger',
  publicationDate: new Date('1951-07-16'),
  isAvailable: true,
};

By using interfaces, you can ensure that your objects have a consistent structure, making your code easier to understand and maintain.

Classes

Classes are another way to define and create objects in TypeScript. A class is like a blueprint for an object, defining its properties and methods. You can then create instances of the class, which are individual objects based on the blueprint.

Here's an example of a Book class:

class Book {
  title: string;
  author: string;
  publicationDate: Date;
  isAvailable: boolean;

  constructor(title: string, author: string, publicationDate: Date) {
    this.title = title;
    this.author = author;
    this.publicationDate = publicationDate;
    this.isAvailable = true;
  }

  checkOut(): void {
    this.isAvailable = false;
  }

  returnBook(): void {
    this.isAvailable = true;
  }
}

In this example, we define a Book class with properties for the title, author, publication date, and availability. We also define a constructor method, which is called when we create a new instance of the class, and two additional methods for checking out and returning the book.

Now, we can create instances of the Book class like this:

let myBook = new Book('The Catcher in the Rye', 'J.D. Salinger', new Date('1951-07-16'));
myBook.checkOut(); // Mark the book as not available
myBook.returnBook(); // Mark the book as available again

By using classes, you can create more organized and reusable code, making it easier to manage large codebases.

Getting Started with TypeScript

Now that you've learned about some of the main features of TypeScript, you might be wondering how to start using it in your own projects. Here's a quick guide to help you get started:

  1. Install TypeScript: To use TypeScript, you'll need to install it on your computer. You can do this using a package manager like npm, which is included with Node.js. If you don't have Node.js installed, you can download it from the official website. Once you have Node.js and npm installed, you can install TypeScript by running the following command in your terminal:

npm install -g typescript

This will install TypeScript globally on your computer, so you can use it in any project.

Create a TypeScript file: Create a new file with the extension .ts, like myFile.ts. You can write TypeScript code in this file, just like you would write JavaScript code in a .js file.

Compile the TypeScript file: To run your TypeScript code in a web browser, you'll need to compile it into plain JavaScript. You can do this by running the following command in your terminal:

tsc myFile.ts

This will create a new file called myFile.js, which contains the compiled JavaScript code. You can include this file in your HTML, just like you would include a normal JavaScript file.

  1. Learn more: There's a lot more to learn about TypeScript, and the best way to learn is by doing. Check out the official TypeScript documentation for more information and examples, and try using TypeScript in your own projects.

We hope this introduction to TypeScript has been helpful and has given you a good foundation to start exploring this powerful programming language. Remember, TypeScript is just an extension of JavaScript, and learning TypeScript will also help you become a better JavaScript developer. Good luck, and happy coding!