What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
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.
Recommended Free Tools
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.
#1 Best Overall
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 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.
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
privatewhen you want a class-local implementation detail checked by TypeScript. - Choose
protectedwhen you deliberately want subclasses to use a member as an inheritance extension point. - Choose
#privatewhen 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.
Do these 3 things before closing this tab:
1Fix the driver behind crashes, sound loss and screen glitches2Clear out junk files and repair common Windows errors3Scan for outdated or missing drivers - takes under a minuteRule 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.
Quick Recap
Best Value
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.




