Skip to content

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 all

TypeScript'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

  • readonly erases too: it blocks assignment through the typed reference, not Object.assign or mutation of the underlying object.
  • static blocks and # fields are both fine under type stripping — they're real JavaScript, not TypeScript inventions.
  • instanceof on classes narrows via the prototype chain — real classes only; structurally identical objects are not instances. Structural typing cuts both ways.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.