📘 TypeScript

Introduction to TypeScript

What is TypeScript?

TypeScript is a strongly typed superset of JavaScript developed by Microsoft. It adds static type checking to JavaScript, catching errors at compile time rather than runtime. TypeScript code compiles to plain JavaScript, so it runs everywhere JavaScript runs.

TypeScript vs JavaScript

// JavaScript — no type checking
function add(a, b) {
    return a + b;
}
add("2", 3)  // "23" — unexpected bug!

// TypeScript — catches the error at compile time
function add(a: number, b: number): number {
    return a + b;
}
add("2", 3)  // ❌ Error: Argument of type 'string' is not assignable to 'number'

Where is TypeScript used?

  • Frontend — Angular (built on TS), React, Vue with TypeScript
  • Backend — Node.js, NestJS, Deno
  • Full-stack — Next.js, Remix, SvelteKit
  • Libraries — Most modern npm packages include TypeScript definitions

Your first TypeScript program

const greeting: string = "Hello, World!";
console.log(greeting);

Why learn TypeScript?

  • 🐞 Catch bugs before they reach production
  • 📝 Better IDE autocomplete and refactoring
  • 🏢 Required at most large tech companies
  • 📚 Self-documenting code with types as documentation