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.
class Counter { #count = 0; inc() { return ++this.#count; } }
Try it
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.
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
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.#secretoutside 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 undernoImplicitAnyit reports error TS7053 (the property does not exist). - Declaring the same private name twice is a duplicate-identifier error, and
delete this.#xis rejected. - Accessibility modifiers (
public,private,protected) cannot be put on a#namemember; the#already means private.
Usage notes
- Target. Native
#fields need no flag for ES2022+ output. For older targets TypeScript emitsWeakMap-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.stringifyorfor...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
#fieldsbreak if called on aProxyof the instance, because the proxy does not have the private slots. - Choosing. Use
#xfor real encapsulation, library code and data that must not leak. Useprivatewhen 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#fieldmakes the class nominal-like for assignment.