TypeScript Tutorial

Classes

class syntax, member visibility, and 'implements' interface

What is the TypeScript class?

In TypeScript, a class is a blueprint (a template) for an object. It defines two things:
  1. Attributes (Data/Fields): What the object "knows."
  2. Methods (Behavior/Actions): What the object "can do."
typescript
class User {
  // Attributes!
  name: string;
  age: number;

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

  // A simple method!
  greet() {
    console.log("Hi, I'm " + this.name + "!");
  }
}

const u1 = new User("Alpha", 25);
u1.greet(); // Run methods!

Comparisons: JavaScript vs. TypeScript Classes

Wait! TypeScript adds several features that don't exist in standard JavaScript:
  • Type Safety: Catching errors if you use the wrong type for an attribute.
  • Visibility: Using public, private, and protected to hide internal data.
  • Interfaces: Forcing a class to follow a specific "contract."

Member Visibility: Who Can See What?

ModifierAccess From AnywhereAccess From SubclassAccess From World
publicYesYesYes
protectedYesYesNo
privateYesNoNo

"Implements" (Joining an Interface)

A class can implement an interface to ensure that it has specific methods and properties.
typescript
interface Drawable {
  draw(): void;
}

// Circle MUST have a draw() method!
class Circle implements Drawable {
  draw() { console.log("Drawing!"); }
}

Static Members

A static member belongs to the class itself, not to individual objects created from it.
typescript
class Utils {
    static APP_VERSION = "1.0.0";
    
    static isValidEmail(email: string) {
        return email.includes("@");
    }
}

console.log(Utils.APP_VERSION); 
console.log(Utils.isValidEmail("hi@example.com"));

Summary

TermExamplesDescription
classclass x {}The blueprint for an object
constructorconstructor()Special method to initialize data
implementsimplements iJoining a formal "interface"
extendsextends pInheriting from a parent class
publicDefaultVisible to everyone
privateprivate xVisible only inside the class
protectedprotected xVisible only to class and children
Best ForStructuring logic, complex business objects, state management
thisRefers to the current object instance
newThe keyword used to create an object
readonlyProperty that cannot be changed after creation
abstractionUse abstract for classes that cannot be instances
OOPEncapsulation, Abstraction, Inheritance, Polymorphism
Supersuper() (Call the parent class constructor!)
ModernTS provides true-private-fields with # (ESNext)
CleanYour class becomes a "Black Box" (Logic stays hidden!)
SafetyTypeScript ensures you don't miss a required method!
OverrideUse override keyword to mark overridden methods
RefactorRename a property, and it updates across all instances!
IdentityAn object never "forgets" its real type internally
DecoratorsModern meta-programming for classes and methods
Getter/SetterMethods used to access private data safely
MemoryObjects live on the "Heap" memory
Garbage CObjects are auto-deleted when no one refers to them!
StandardEstablished and trusted worldwide for 10+ years
SafeTS forces you to initialize ALL non-optional fields!
LogicOne interface, many implementations
DesignAllows for "Liskov Substitution Principle" (SOLID)