TypeScript Tutorial

TypeScript Access Specifiers

public, private, and protected control who can read and write members.

Who can touch a member

Access specifiers sit on each field and method. public members are open to any code that has an object. private members are only for methods (and constructors) of that class.protected sits between those two when inheritance is in play.

Put data you want to protect in private, and the functions other files should call in public. Unlike C++, TypeScript does not use public: section labels. You write the word on the member itself.

public

Public members are the class's outer surface. The rest of the file can read and write them. That was useful while you learned dots and objects. It is a poor default for real fields: any line of code can setscore = -9 and break a rule the class was supposed to keep.

Example

class Meter {
  public reading: number = 0;

  public show(): void {
    console.log(this.reading);
  }
}

const m: Meter = new Meter();
m.reading = 7;
m.show();

Members with no specifier are already public. Writing public makes the choice visible. This tutorial keeps the word on purpose.

private

Private members are invisible outside the class. The rest of the file cannot write v.code. It must call a public method such as setCode. Methods inside Vault can still usethis.code directly.

Example

class Vault {
  private code: number = 0;

  public setCode(c: number): void {
    this.code = c;
  }

  public getCode(): number {
    return this.code;
  }
}

const v: Vault = new Vault();
v.setCode(42);
console.log(v.getCode());

Paste this into /typescript/try, then add v.code = 0; after new Vault()and compile. tsc should reject it: code is private.

public vs private

publicprivate
Methods of the same classCan read and writeCan read and write
Code outside the classCan read and writeCannot; compile error
Typical useFunctions other code should callFields and helpers
Class defaultYes — omit the word and it is still publicNo — you must write private

A class with no specifier at all treats members as public. That is why these lessons writeprivate the moment a field should stay inside the type.

protected

protected is like private for code outside the family of classes, but a derived class can use those members. You need extends to see the difference. Until the inheritance chapter, use public for the interface and private for the data.

Example

class Account {
  protected balance: number = 0;

  public getBalance(): number {
    return this.balance;
  }
}

class Savings extends Account {
  public deposit(n: number): void {
    this.balance = this.balance + n;
  }
}

const s: Savings = new Savings();
s.deposit(20);
console.log(s.getBalance());

Savings may write this.balance because the field is protected.s.balance = 0 from outside both classes would not compile. Do not mark fields protected just to reach them from the top of the file. That is not what protected is for.

A constructor can set private fields

Constructors belong to the class, so they may assign private members. That is the clean way to start aVault with a code: pass it into the constructor, store it in the private field, never expose the field itself.

Example

class Vault {
  private code: number;

  constructor(c: number) {
    this.code = c;
  }

  public getCode(): number {
    return this.code;
  }
}

const v: Vault = new Vault(42);
console.log(v.getCode());

Hide the data, publish the operations

Access specifiers are the first half of encapsulation. The next chapter puts that into a habit: private fields, public getters and setters, and rules the class enforces in one place.

FAQ: TypeScript Access Specifiers

Common questions about this page.

What is the StudyGrid TypeScript tutorial?

The StudyGrid TypeScript tutorial follows the same chapter rhythm as C++: syntax, types, input, loops, functions, classes, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run typescript access specifiers examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn typescript access specifiers in this TypeScript TypeScript lesson (TypeScript Access Specifiers).

Is the TypeScript editor the same as Try Python or Try C++?

No. Try TypeScript type-checks with tsc at /typescript/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. TypeScript lessons never open those editors.

Do I need to install a compiler to learn TypeScript?

No. Open a chapter, click Try it in TypeScript, and compile in the browser. You can also download a .ts file and compile locally with tsc.

Where should I start the TypeScript tutorial?

Start at TypeScript Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open TypeScript Examples, then generics, Map, and arrow functions. Use Next at the bottom of each chapter.

Is the TypeScript tutorial free?

Yes. The TypeScript workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.