No history yet

Introduction to TypeScript

What Is TypeScript?

Think of TypeScript as JavaScript with superpowers. It's a programming language built by Microsoft that adds optional static types to JavaScript. The key phrase here is "superset of JavaScript," which means any valid JavaScript code is also valid TypeScript code. You don't have to learn an entirely new language from scratch.

The magic of TypeScript happens during development. You write your code in TypeScript files (ending in .ts), and then a tool called the TypeScript compiler transforms it into plain, clean, browser-compatible JavaScript. This means you get the benefits of a more robust language while still running standard JavaScript everywhere.

Why Bother with Types?

Adding types might seem like extra work, but it pays off significantly, especially in larger projects. Here are the main advantages:

  • Catch Errors Early: With types, the compiler can spot mistakes before you even run your code. Imagine trying to add a number to a string by accident. JavaScript might produce a weird result at runtime, but TypeScript will flag it immediately in your editor. It's like having a grammar checker for your logic.
  • Improved Code Quality and Readability: When you see a function, you know exactly what kind of data it expects and what it returns. This makes your code easier to understand, refactor, and maintain, both for you and for your teammates.
  • Smarter Tools: Because TypeScript understands the shape of your data, code editors can provide much better autocompletion, navigation, and error checking. This speeds up your development process and reduces the mental load of remembering every detail of your codebase.
Lesson image

Setting Up Your Environment

Getting started with TypeScript is straightforward. You'll need Node.js and its package manager, npm, which you likely already have if you've done any modern web development. Once that's set up, you can install TypeScript globally on your system using a simple terminal command.

npm install -g typescript

This command gives you access to the TypeScript compiler, tsc. To check if it installed correctly, you can run tsc -v in your terminal, which should print the installed version number.

Most TypeScript projects also include a configuration file named tsconfig.json. This file lives in the root of your project and tells the compiler how you want it to behave—for instance, which files to include and what version of JavaScript to output.

{
  "compilerOptions": {
    "target": "es2016",
    "module": "commonjs",
    "outDir": "./dist",
    "strict": true
  },
  "include": ["src/**/*"]
}

In this example configuration, we're telling the compiler to:

  • target: Compile our code down to ES2016-compatible JavaScript.
  • module: Use the CommonJS module system.
  • outDir: Place the compiled JavaScript files into a dist directory.
  • strict: Enable a set of strict type-checking rules for higher code quality.
  • include: Only compile files found within the src directory.

TypeScript in Action

Let's see a simple example. Here’s a basic function written in TypeScript. Notice the : string part. This is a type annotation, telling TypeScript that the person parameter must be a string.

// main.ts
function greet(person: string) {
  return "Hello, " + person;
}

let user = "Jane User";
document.body.textContent = greet(user);

If you tried to call greet(123), TypeScript would show an error right in your editor. To compile this file, you'd run the following command in your terminal:

tsc main.ts

This creates a new file, main.js, with the type annotations removed, ready to run in any browser.

// main.js
function greet(person) {
    return "Hello, " + person;
}
var user = "Jane User";
document.body.textContent = greet(user);

One of TypeScript's biggest strengths is its seamless integration with existing JavaScript projects. Since all JavaScript is valid TypeScript, you can adopt it gradually. A common approach is to rename a .js file to .ts and slowly start adding types. This allows you to introduce the benefits of TypeScript into a large codebase without having to rewrite everything at once.

Start small. You can convert one file at a time to begin reaping the benefits of type safety without a major overhaul.

Ready to check your understanding?

Quiz Questions 1/5

What is the primary relationship between TypeScript and JavaScript?

Quiz Questions 2/5

What is the main benefit of using types in a TypeScript project?