Course contentsShow
← TypeScript
Lesson 1047 of 1,79624. Classes in TypeScriptFree lesson

Private Members and implements

Interfaces cannot require private or protected members; implements only checks public shape.

Private Members and implements

What you'll learn: Why interfaces can't require private or protected members, and how implements checks only the public shape of a class.

Why This Matters

When you use the implements keyword to ensure a class adheres to an interface, TypeScript performs a critical but limited check: it only verifies the public API of your class. Interfaces cannot declare private or protected members, and attempting to do so will result in a compiler error.

The Core Rule

Interfaces define contracts for the public shape of objects. Since other code cannot access private or protected members anyway, it wouldn't make sense for an interface to require them. The implements clause checks that your class provides all the public properties and methods the interface demands — nothing more.

interface Storeable {
  save(): void;
  // private data: string;  // ❌ Error: 'private' modifier cannot appear on a type member
}

class User implements Storeable {
  private internalId: number = 0;  // ✅ Fine — interface doesn't care
  protected email: string = "";     // ✅ Fine — interface doesn't care

  public save(): void {
    // Implementation using private members
    console.log(`Saving user ${this.internalId}`);
  }
}

What This Means in Practice

Your class can have as many private or protected members as it needs for internal implementation. The interface only cares that the public methods and properties it requires are present and correctly typed. This separation keeps implementation details hidden while enforcing external contracts.

Think of it like a restaurant menu: the menu (interface) lists what dishes you can order (public methods), but it doesn't dictate what equipment is in the kitchen (private members) — that's the chef's business.

Key Takeaway: Interfaces only enforce public shape; implements ignores private and protected members entirely, letting you hide implementation details while still fulfilling contracts.