DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk3 min

Protected vs. Private in TypeScript: What’s the Difference?

In TypeScript, protected members are accessible to subclasses while private members are not. Neither modifier enforces runtime privacy; ECMAScript #private fields do.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

protected lets a class and its subclasses access a member; private restricts typed access to the class that declares it. Both are TypeScript checks, not runtime security barriers. If you need runtime-enforced privacy, use ECMAScript #private fields instead.

How private and protected differ

TypeScript class members are public by default. The two modifiers in this comparison both restrict ordinary access from outside the class, but they differ in whether a subclass may use the member.

As an Amazon Associate I earn from qualifying purchases.

Access question private protected
Can the declaring class access it? Yes Yes
Can a subclass access it? No Yes, subject to inheritance rules
Can unrelated code access it through ordinary typed access? No No
Does the modifier enforce privacy at runtime? No No

Use private when a member is an implementation detail that subclasses should not depend on. Use protected when subclasses are intentionally meant to use or extend that part of the class.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Example: a subclass can use protected, not private

class Base {
  private cacheKey = "base";

  protected format(value: string) {
    return `[${value}]`;
  }
}

class Child extends Base {
  render() {
    return this.format("hello"); // allowed
    // return this.cacheKey;     // type error: private in Base
  }
}

const child = new Child();
// child.format("hello");        // type error: protected

The child may call format from its own implementation, but code outside the class hierarchy cannot call it through a Child instance. The private cacheKey remains inaccessible to the subclass through normal typed access.

Protected access follows the inheritance hierarchy

protected does not mean that every object related to a base class can access the member. TypeScript restricts protected access to the declaring class and derived-class implementations; it rejects some cross-hierarchy access through an instance of a sibling subclass.

A subclass also cannot make a base class’s private member accessible by redeclaring a member with the same name. Within a class body, however, TypeScript permits one instance of that same class to access a private member on another instance of that class.

Rank #2
TypeScript Programming Language - Software Engineer & Coder T-Shirt
  • TypeScript implements a superset of syntax for strictly typed development, facilitating deep static analysis and enhanced development environment integration. The compiler translates source into standard script formats, ensuring parity across any runtime.
  • TypeScript is ideal for front-end developers, full-stack engineers, and software architects who build large-scale web applications. It serves those looking to improve code excellence, reduce bugs through static checking, and maintain complex projects more.
  • Lightweight, Classic fit, Double-needle sleeve and bottom hem

TypeScript modifiers are not runtime privacy

The TypeScript Handbook explains: “Like other aspects of the type system, private and protected are only enforced during type checking.” The modifiers are erased from emitted JavaScript, so ordinary JavaScript property lookup can still reach a property declared with either modifier. TypeScript also permits bracket notation to access a soft-private member, such as instance["cacheKey"], in circumstances where dot access is rejected.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

That makes these modifiers useful for defining and checking a class’s intended API, not for hiding secrets or creating a security boundary. Do not store credentials or other sensitive data in a normal property on the assumption that private or protected makes it inaccessible at runtime.

When to use ECMAScript #private

ECMAScript private fields use a hash-prefixed name, such as #secret. Unlike TypeScript’s access modifiers, these private names are enforced at runtime and are not available through ordinary property lookup or bracket notation. A subclass cannot access a private name declared by its base class.

  • Choose private when you want a class-local implementation detail checked by TypeScript.
  • Choose protected when you deliberately want subclasses to use a member as an inheritance extension point.
  • Choose #private when you need runtime-enforced encapsulation and your compilation target and supported runtimes can accommodate it.

TypeScript’s 3.8 release notes describe support for #private and note that downlevel implementation requires an ES2015/ES6 target or higher. Check the project’s current target and runtime support before adopting the syntax. TypeScript 4.3 later added support for private methods and accessors using # names. See the TypeScript 3.8 release notes and TypeScript 4.3 release notes.

Visibility can affect type compatibility

Private and protected members affect whether class instances are assignable to one another. For compatibility, corresponding private or protected members must originate from the same declaration. Two classes with otherwise similar shapes may therefore be incompatible if their members come from unrelated declarations. The TypeScript Handbook’s type compatibility guide covers this rule.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Rule of thumb

Prefer private unless subclasses have a deliberate reason to rely on a member. Every protected member becomes part of the class’s inheritance-facing design; use #private instead when the requirement is runtime privacy rather than a TypeScript-only access check. The TypeScript Classes Handbook documents the visibility and inheritance rules.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.