Lesson 5 of 14 · 18m
Classes: `private` is a comment, `#private` is a wall
TypeScript's access modifiers erase at emit, ECMAScript #fields are enforced by the engine — and parameter properties aren't erasable.
Mark a field private in TypeScript, compile, run obj["secret"] at runtime, and the secret prints. private never reaches the emitted JavaScript — it's erased with the rest of the type syntax. What actually enforces privacy in JavaScript is the #field syntax from ES2022, and confusing the two is how API tokens end up in logs.
private is a lint rule, #private is a wall
class Session {
private token = "s3cr3t"; // compile-time only
#key = crypto.randomUUID(); // engine-enforced
}
const s = new Session();
(s as any).token; // works — private erased at emit
(s as any)["#key"]; // nothing — #key isn't a string key at allTypeScript's private/protected/readonly are access annotations. They shape what the compiler allows through that reference; at runtime the object is a normal object and Object.keys, JSON.stringify, and plain bracket access all reach in. #field is different machinery entirely — hard privacy enforced by the JS engine itself, stored in an internal slot keyed to the instance. No reflection reaches it, no for...in lists it, JSON.stringify skips it, and a subclass can't see it. It shipped in ES2022 and has been in Node since v16.4.
The threat-model consequence is real: if a field holds a credential, a signing key, or a decrypted secret, private leaves it one (obj as any) or for...in away from a stack trace or a serializer. # removes the property from the observable surface entirely. private is how you say "not part of the API." # is how you make it true.
There's a second, quieter payoff: #key in obj is a real operator that tests whether obj carries that private field — a brand check that can't be forged from outside the class. Libraries use #field in x as a genuine "was this made by us" test, which is stronger than instanceof (forgeable via Object.setPrototypeOf) and works across realms. If the boundary lesson's branding pattern feels like a workaround, this is the built-in version — private fields are the only nominal identity JavaScript actually enforces.
Parameter properties: the sugar that isn't erasable
class Repo {
constructor(private readonly db: Db) {} // declares + assigns db
}Parameter properties declare and assign a field in one stroke — convenient, and non-erasable syntax: expanding private db into a field plus an assignment is codegen, not annotation-stripping. They're on Node's type-stripping banned list alongside enum, and erasableSyntaxOnly flags them. If you run through Node strip-types, ts-blank-space, or Amaro, write it longhand:
class Repo {
readonly #db: Db;
constructor(db: Db) { this.#db = db; }
}implements and override: shape checks, not guarantees
class Postgres implements Store is an assignability check on the class's shape — useful because the error lands at the class declaration instead of at every call site. It adds zero runtime behavior, and it doesn't make the class nominal: a plain { get, set, delete } object still satisfies Store structurally.
For real hierarchies, two things carry the load. noImplicitOverride — a standalone flag since TS 4.3, not bundled into strict — requires the override keyword on methods that override a base, turning "I renamed the base method and three subclasses silently stopped overriding" into a compile error. And strictFunctionTypes doesn't apply to methods — method parameters are still checked bivariantly, a deliberate unsoundness kept for ergonomics. Standalone function types are strict; methods get slack. Know which you're writing.
Try this
Take a class holding a secret with private. Confirm instance["secret"] reads it under ts-node/tsx or after tsc emit. Convert to # and watch both the property-access error and the changed JSON.stringify output.
Pitfalls
readonlyerases too: it blocks assignment through the typed reference, notObject.assignor mutation of the underlying object.staticblocks and#fields are both fine under type stripping — they're real JavaScript, not TypeScript inventions.instanceofon classes narrows via the prototype chain — real classes only; structurally identical objects are not instances. Structural typing cuts both ways.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
Building a Modern App With React, Vite, and Tailwind CSS: The Complete 2025 Guide
React for the UI, Vite for the build tooling, Tailwind for the styles. This is the stack most new frontend projects start with in 2025. This guide wires all three together from scratch and builds a real application structure you can actually start a production project from.
What is Vite? The Build Tool That Made Frontend Development Fast Again
Vite replaced Webpack for a lot of teams without much fanfare. Native ES modules, a dev server that starts in milliseconds, and a production build that doesn't require understanding 300 lines of config. Here's how it works and why the switch is worth it.
What is React? The JavaScript Library That Changed Frontend Development
React is a JavaScript library for building user interfaces. Facebook built it in 2013, open-sourced it, and the entire frontend ecosystem reorganized itself around it. Here is how the component model works, why it took over, and what you need to know before writing a single line.
Angular 18: Deferred Views & Built-in Control Flow
The newest release of Angular – which is Angular 18 – is planned to be released on May 22nd, 2024. This is Google's super popular TypeScript based webapp framework that allows developers to create amazing web applications. The features, changes, and goodies of this release are deferrable views…