Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Keywords and Declarations

TypeScript #private fields

Reference for #private class members in TypeScript: ECMAScript private fields and methods, runtime enforcement, the in operator brand check, and private vs #private.

A name that starts with # declares a private class member as defined by JavaScript itself: #count, #log(), static #instances. Only code written inside the class body can use it. Unlike TypeScript's private modifier, the engine enforces this at runtime, so outside code cannot reach the value by any means, including bracket access or reflection.

TypeScript understands these members fully: it types them, checks that you only use them inside the class, and supports #x in obj as a type-narrowing brand check.

TypeScript
class Counter {
  #count = 0;
  inc() { return ++this.#count; }
}

Try it

TypeScript Truly private state

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript Outside access fails

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Syntax

TypeScript
class C {
  #field: Type = init;
  #method(): void {}
  get #acc(): T { /* ... */ }
  static #shared = 1;
  static { /* may use C.#shared */ }
  has(o: object) { return #field in o; }
}
Part Description
#name The # is part of the name. #a and a are different members and can coexist.
#name in obj Brand check: true if obj was created by this class (has that private slot). Narrows obj to the class type.

Errors

  • Using v.#secret outside the class: error TS18013 ("Property '#secret' is not accessible outside class 'Vault' because it has a private identifier").
  • Bracket access v["#secret"] is just a string key, so under noImplicitAny it reports error TS7053 (the property does not exist).
  • Declaring the same private name twice is a duplicate-identifier error, and delete this.#x is rejected.
  • Accessibility modifiers (public, private, protected) cannot be put on a #name member; the # already means private.

Usage notes

  • Target. Native # fields need no flag for ES2022+ output. For older targets TypeScript emits WeakMap-based helpers; very old targets (below ES2015) are not supported.
  • Not visible to subclasses. A subclass cannot read this.#x, but it can declare its own #x; both exist separately.
  • Invisible to reflection. Private names do not show up in Object.keys, JSON.stringify or for...in, and legacy (experimental) decorators cannot be applied to them.
  • Per-instance cost. Each instance holds the slot; arrays of millions of small objects should be measured before adopting any private-field pattern, though modern engines optimize them well.
  • Proxies. Methods using #fields break if called on a Proxy of the instance, because the proxy does not have the private slots.
  • Choosing. Use #x for real encapsulation, library code and data that must not leak. Use private when you want a bracket-access escape hatch (for example in tests) or need the member to work with legacy decorators.
  • Structural typing. Like private, a #field makes the class nominal-like for assignment.