Runtime vs Compile-Time Privacy
You'll learn that TypeScript's public, private, and protected modifiers only enforce privacy during compilation—they vanish completely at runtime and don't protect your data in running JavaScript code.
The Core Misunderstanding
When you mark a property as private in TypeScript, it feels like you're creating genuine privacy, similar to what you'd expect in Java or C#. But here's the crucial truth: TypeScript's access modifiers are purely compile-time constructs. They're erased during compilation and have zero effect on the JavaScript that actually runs.
What This Means in Practice
Think of TypeScript's access modifiers like spell-check in a word processor. The spell-checker prevents you from submitting a document with typos while you're writing, but once the document is printed, the spell-checker isn't embedded in the paper protecting the words. Similarly, private stops you from accessing properties while writing TypeScript, but the compiled JavaScript has no privacy enforcement whatsoever.
When TypeScript compiles your class with private properties to JavaScript, those properties become regular, publicly accessible properties. Anyone with access to your compiled JavaScript can read and modify them directly—through the browser console, in bundled code, or via dynamic property access.
Why TypeScript Works This Way
TypeScript is a type system, not a runtime security mechanism. Its job is to catch mistakes during development, not to enforce access control when your code runs. Since JavaScript (until recently with #private fields) had no native privacy mechanism, TypeScript couldn't magically create one. The access modifiers exist to help you write better code, prevent accidental misuse, and communicate intent—not to secure data.
The Practical Implication
Use private for developer ergonomics and API design, not for security. It prevents teammates from accidentally depending on internal implementation details. But never rely on it to hide sensitive information—anyone who can run your JavaScript can access everything.
Key Takeaway: TypeScript access modifiers (public, private, protected) are compile-time only—they guide developers but provide zero runtime privacy or security.