ECMAScript private fields are a native JavaScript feature, fully supported in TypeScript, that provides strict, parse-time and runtime-enforced encapsulation for class members. By prefixing a class property, method, or accessor identifier with a hash symbol (Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
#), the member becomes completely inaccessible outside the lexical scope of its containing class body.
Technical Characteristics
Strict Enforcement Unlike TypeScript’sprivate modifier, which is a compile-time only construct erased during JavaScript emission, ECMAScript private fields (#) guarantee hard privacy. Attempting to access a # field outside its class results in a SyntaxError in JavaScript. Because this is a parse-time error, it prevents the script from executing entirely. Furthermore, the field cannot be inspected using standard property enumeration (e.g., Object.keys()) or reflection.
No Dynamic Access
Private fields cannot be accessed dynamically using bracket notation. The identifier must be statically known and accessed via dot notation within the lexical scope of the class. Attempting to use bracket notation with a string literal does not interact with the private field at all; it merely attempts to access a standard public property whose string key happens to include a hash.
Compilation and Target Environments
TypeScript’s handling of ECMAScript private fields depends on thetarget specified in the tsconfig.json:
ES2022or higher: TypeScript emits the native#syntax directly into the compiled JavaScript.ES2015toES2021: TypeScript downlevels the private fields usingWeakMapinstances to simulate strict runtime privacy without leaking memory.- Below
ES2015(e.g.,ES5): ECMAScript private fields are not supported, and compilation will fail, asWeakMapis required for polyfilling the behavior.
Master TypeScript with Deep Grasping Methodology!Learn More





