Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons

© 2026 BraveProgrammer. All rights reserved.

  1. Courses
  2. /
  3. TypeScript Mastery

Lesson 17 of 56 · Typescript

Classes – Public, Private, Protected

Duration: 25 minutes

Class Syntax

class Person {
  public name: string;
  private ssn: string; // hidden from outside
  protected birthYear: number; // visible to subclasses

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

  public getAge(currentYear: number): number {
    return currentYear - this.birthYear;
  }
}

Private Fields (# syntax – stage 4)

class Secret {
  #token: string;
  constructor(token: string) { this.#token = token; }
  reveal() { return this.#token; }
}

Access Modifiers Summary

ModifierVisibility
publicAnywhere (default)
protectedClass and subclasses
privateClass only
#privateClass only, enforced at runtime

Info

When targeting older browsers, private fields compile down to closure‑based emulation – be aware of the output size.

Previous: Recursive TypesNext: Class Inheritance & Abstract Classes