T

Classes

TypeScript syntax guide

Object-oriented programming with typed classes in TypeScript

Classes

Object-oriented programming with typed classes in TypeScript

TypeScript classes (typescript)
        
          // Class with type annotations
class Person {
  // Property types
  name: string;
  private age: number;
  protected email: string;

  // Constructor
  constructor(name: string, age: number, email: string) {
    this.name = name;
    this.age = age;
    this.email = email;
  }

  // Method
  greet(): string {
    return `Hello, I'm ${this.name}`;
  }

  // Getter/Setter
  get isAdult(): boolean {
    return this.age >= 18;
  }

  set updateAge(newAge: number) {
    if (newAge >= 0) {
      this.age = newAge;
    }
  }

  // Static method
  static createAnonymous(): Person {
    return new Person("Anonymous", 0, "anonymous@example.com");
  }
}

// Inheritance
class Employee extends Person {
  jobTitle: string;
  salary: number;

  constructor(name: string, age: number, email: string, jobTitle: string, salary: number) {
    super(name, age, email);
    this.jobTitle = jobTitle;
    this.salary = salary;
  }

  work(): string {
    return `${this.name} is working as ${this.jobTitle}`;
  }

  // Method override
  greet(): string {
    return `${super.greet()} and I work as ${this.jobTitle}`;
  }
}

// Abstract classes
abstract class Shape {
  abstract getArea(): number;

  getDescription(): string {
    return `Area: ${this.getArea()}`;
  }
}

class Rectangle extends Shape {
  constructor(private width: number, private height: number) {
    super();
  }

  getArea(): number {
    return this.width * this.height;
  }
}

// Usage
const person = new Person("John", 25, "john@example.com");
const employee = new Employee("Jane", 30, "jane@example.com", "Developer", 75000);
const rect = new Rectangle(10, 20);

console.log(person.greet()); // Hello, I'm John
console.log(employee.greet()); // Hello, I'm Jane and I work as Developer
console.log(rect.getArea()); // 200
        
      

Explanation

TypeScript extends JavaScript classes with features like access modifiers (public, private, protected) and interfaces for class implementation, promoting robust object-oriented design.

Common Use Cases

  • Building complex applications with clear object structures
  • Implementing design patterns
  • Creating reusable components

Related TypeScript Syntax

Master Classes in TypeScript

Understanding classes is fundamental to writing clean and efficient TypeScript code. This comprehensive guide provides you with practical examples and detailed explanations to help you master this important concept.

Whether you're a beginner learning the basics or an experienced developer looking to refresh your knowledge, our examples cover real-world scenarios and best practices for using classes effectively in your TypeScript projects.

Key Takeaways

  • Building complex applications with clear object structures
  • Implementing design patterns
  • Creating reusable components