T

Classes

TypeScript Syntax Guide

Programação orientada a objetos com classes tipadas em TypeScript

Classes

Programação orientada a objetos com classes tipadas em 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 estende as classes JavaScript com recursos como modificadores de acesso (public, private, protected) e interfaces para implementação de classe, promovendo design orientado a objetos robusto.

Common Use Cases

  • Construir aplicações complexas com estruturas de objetos claras
  • Implementar padrões de design
  • Criar componentes reutilizáveis

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

  • Construir aplicações complexas com estruturas de objetos claras
  • Implementar padrões de design
  • Criar componentes reutilizáveis