TypeScript Tutorial

Learn TypeScript from basics to advanced

🚀 Welcome to TypeScript

TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tooling at any scale. It adds optional static typing, classes, and interfaces to help catch errors early and write more maintainable code.


// Your first TypeScript code
let message: string = "Hello, TypeScript!";
console.log(message);
                                    

Why Learn TypeScript?

🛡️

Type Safety

Catch errors before runtime

let age: number = 25;
// age = "25"; // Error!
💡

Better IDE Support

Autocomplete and IntelliSense

interface User {
  name: string;
  age: number;
}
📚

Modern JavaScript

Use latest JS features

const greet = (name: string) => {
  return `Hello, ${name}!`;
};
🔧

Easy Refactoring

Rename and restructure safely

type Status = "active" | "inactive";
let userStatus: Status = "active";

🔹 TypeScript vs JavaScript

See the difference between JavaScript and TypeScript:

🔸 JavaScript

// JavaScript - No type checking
function add(a, b) {
    return a + b;
}

add(5, 10);      // 15
add("5", "10");  // "510" - Unexpected!

🔸 TypeScript

// TypeScript - Type safe
function add(a: number, b: number): number {
    return a + b;
}

add(5, 10);      // 15
// add("5", "10"); // Error: Type 'string' not assignable to 'number'

🔹 What You'll Learn

  • Introduction: Understanding TypeScript basics
  • Get Started: Setting up your development environment
  • Configuration: Configuring TypeScript compiler
  • Null & Undefined: Handling null and undefined values
  • TS 5 Updates: Latest features in TypeScript 5

🔹 Quick Example

Here's a simple TypeScript example showing type annotations:

// Define a type for a person
interface Person {
    name: string;
    age: number;
    email?: string; // Optional property
}

// Create a person object
const user: Person = {
    name: "John Doe",
    age: 30
};

// Function with typed parameters
function greetPerson(person: Person): string {
    return `Hello, ${person.name}! You are ${person.age} years old.`;
}

console.log(greetPerson(user));
// Output: Hello, John Doe! You are 30 years old.

🔹 Getting Started

Ready to start learning TypeScript? Follow these steps:

  1. Read the Introduction to understand TypeScript fundamentals
  2. Follow the Get Started guide to install TypeScript
  3. Learn about Configuration options
  4. Explore advanced topics like Null & Undefined
  5. Discover the latest TS 5 Updates

🧠 Test Your Knowledge

What is TypeScript?