TypeScript has gone from a niche Microsoft experiment to the default choice for serious JavaScript projects. In the 2024 State of JS survey, over 80% of respondents said they use TypeScript regularly. Major frameworks like Angular, Next.js, and SvelteKit ship TypeScript-first. If you write JavaScript professionally, learning TypeScript is no longer optional — it's expected.

What Is TypeScript?

TypeScript is a superset of JavaScript created by Microsoft. Every valid JavaScript file is also valid TypeScript. TypeScript adds one major feature: a static type system that catches errors at compile time instead of runtime. The TypeScript compiler (tsc) strips out all type annotations and produces plain JavaScript that runs anywhere — Node.js, browsers, Deno, or Bun.

Why Does TypeScript Exist?

JavaScript was designed for small scripts — adding interactivity to web pages. As applications grew to millions of lines of code (think Gmail, VS Code, Figma), JavaScript's dynamic typing became a liability. Bugs that a statically-typed language would catch at compile time — accessing properties on undefined, passing wrong argument types, misspelling object keys — only surfaced at runtime, often in production.

TypeScript solves this by giving JavaScript a compile-time type checker. You still write JavaScript-like code, but the compiler verifies that your types are consistent before you ship.

TypeScript vs JavaScript: Key Differences

JavaScript (no type safety)
function greet(name) {
  return "Hello, " + name.toUpperCase();
}

greet(42); // Runtime error: name.toUpperCase is not a function
TypeScript (caught at compile time)
function greet(name: string): string {
  return "Hello, " + name.toUpperCase();
}

greet(42); // ❌ Compile error: Argument of type 'number'
           // is not assignable to parameter of type 'string'

Core TypeScript Concepts

1. Type Annotations

The most basic TypeScript feature. You annotate variables, parameters, and return types with their expected type.

Basic Type Annotations
let username: string = "Alice";
let age: number = 30;
let isAdmin: boolean = true;
let scores: number[] = [95, 87, 92];

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

2. Interfaces

Interfaces define the shape of objects. They're one of TypeScript's most powerful features for documenting and enforcing API contracts.

Interfaces
interface User {
  id: number;
  name: string;
  email: string;
  role: "admin" | "editor" | "viewer";
  lastLogin?: Date; // optional property
}

function sendWelcome(user: User): void {
  console.log(`Welcome, ${user.name}!`);
}

// ❌ Compile error if you forget a required property
sendWelcome({ id: 1, name: "Alice" }); // Missing 'email' and 'role'

3. Union Types & Literal Types

Union types allow a value to be one of several types. Combined with literal types, they create precise, self-documenting type definitions.

Union & Literal Types
type Status = "loading" | "success" | "error";
type ID = string | number;

function showStatus(status: Status): string {
  switch (status) {
    case "loading": return "⏳ Loading...";
    case "success": return "✅ Done!";
    case "error":   return "❌ Failed";
  }
}

4. Generics

Generics let you write reusable code that works with any type while preserving type safety.

Generics
function firstElement<T>(arr: T[]): T | undefined {
  return arr[0];
}

const num = firstElement([1, 2, 3]);       // type: number
const str = firstElement(["a", "b", "c"]); // type: string

Getting Started

Install & Configure
# Install TypeScript
npm install -D typescript

# Create tsconfig.json
npx tsc --init

# Compile TypeScript to JavaScript
npx tsc

# Watch mode (recompile on save)
npx tsc --watch

The tsconfig.json file controls compiler options. Start with "strict": true — it enables the full suite of type checks and is considered best practice for all new projects.

Common Mistakes to Avoid

  • Using any everywhere: This defeats the purpose of TypeScript. If you find yourself using any, you probably need a proper interface or generic.
  • Ignoring compiler errors: Don't use @ts-ignore as a habit. Fix the underlying type issue.
  • Over-engineering types: Start simple. You don't need advanced mapped types or conditional types for most application code.
  • Not using strict mode: Non-strict TypeScript catches far fewer bugs. Always enable strict mode.

When to Use TypeScript

  • Any project with more than one developer
  • Libraries and packages published to npm
  • Codebases expected to live longer than 6 months
  • Projects using frameworks that support it natively (Next.js, Angular, SvelteKit)

Try Our Free Developer Tools

Minify, beautify, and encode your code instantly in your browser.