TypeScript private Keyword (Compile-Time Privacy)
Using the private keyword enforces visibility strictly during the TypeScript compilation phase. At runtime, the compiled JavaScript exposes the getter on the prototype, making it technically accessible if type-checking is bypassed.
ECMAScript # Identifier (Runtime Privacy)
Using the ECMAScript # prefix creates a hard private getter. This enforces strict encapsulation at both compile-time and runtime, ensuring the getter cannot be accessed or inspected outside the class via JavaScript reflection or prototype manipulation.
Key Characteristics
- Read-Only Nature: A private getter without a corresponding private setter creates a strictly read-only property within the class context. Attempting to assign a value to it internally will result in a compiler error.
- Subclass Restriction: Unlike the
protectedmodifier, neitherprivatenor#getters can be accessed by classes extending the base class. - Memory Allocation: Standard getters are defined on the class prototype. However, ECMAScript private getters (
#) utilize internal slots orWeakMapimplementations under the hood in the JavaScript engine to guarantee runtime privacy, meaning they are not enumerable or accessible viaObject.getPrototypeOf().
Tired of Poor TypeScript Skills? Fix That With Deep Grasping!Learn More





