Introduction to TypeScript


TypeScript has rapidly become one of the most popular programming languages in the web development ecosystem. But what exactly is TypeScript, and why should you care? Let’s dive in.

What is TypeScript?

TypeScript is a strongly typed programming language that builds on JavaScript. It’s often described as a “superset” of JavaScript, meaning any valid JavaScript code is also valid TypeScript code. The key difference is that TypeScript adds optional static type checking and other features that help catch errors during development rather than at runtime.

Why Use TypeScript?

1. Catch Errors Earlier

With TypeScript, many common errors are caught at compile time rather than runtime:

function greet(name: string) {
  console.log(`Hello, ${name}!`);
}

greet(123); // Error: Argument of type 'number' is not assignable to parameter of type 'string'

2. Better IDE Support

TypeScript enables powerful autocompletion, refactoring tools, and inline documentation in your code editor. Your IDE can provide intelligent suggestions because it understands the types in your code.

3. Self-Documenting Code

Type annotations serve as inline documentation:

interface User {
  id: number;
  name: string;
  email: string;
  isActive: boolean;
}

function getUser(id: number): Promise<User> {
  // Implementation
}

Just by looking at the function signature, you know exactly what it expects and returns.

4. Easier Refactoring

When you rename a property or function, TypeScript helps you find all usages throughout your codebase. No more “find and replace” mishaps.

Getting Started

Installing TypeScript is simple:

npm install -g typescript

Create a TypeScript file (hello.ts):

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

console.log(sayHello("World"));

Compile it to JavaScript:

tsc hello.ts

This creates a hello.js file that can run in any JavaScript environment.

Basic Types

TypeScript includes several basic types:

let isDone: boolean = false;
let count: number = 42;
let username: string = "Alice";
let list: number[] = [1, 2, 3];
let tuple: [string, number] = ["hello", 10];

Interfaces

Interfaces define the structure of objects:

interface Person {
  firstName: string;
  lastName: string;
  age?: number; // Optional property
}

function greetPerson(person: Person) {
  console.log(`Hello, ${person.firstName} ${person.lastName}!`);
}

Type Inference

TypeScript is smart enough to infer types in many cases:

let message = "Hello"; // TypeScript infers this is a string
message = 42; // Error: Type 'number' is not assignable to type 'string'

Union Types

Sometimes a value can be one of several types:

function printId(id: string | number) {
  console.log(`Your ID is: ${id}`);
}

printId(101);      // OK
printId("202");    // OK

Should You Use TypeScript?

TypeScript is particularly valuable for:

  • Large codebases with multiple developers
  • Projects that need to be maintained long-term
  • Libraries and frameworks used by others
  • Teams that want to catch bugs early

You might skip TypeScript for:

  • Small, short-lived scripts
  • Quick prototypes
  • Solo projects where you prefer rapid iteration

Next Steps

  1. Try the TypeScript Playground
  2. Read the official TypeScript Handbook
  3. Convert a small JavaScript project to TypeScript
  4. Explore advanced features like generics and decorators

TypeScript has a learning curve, but most developers find the investment pays off in improved code quality and developer experience. Give it a try on your next project!