#private vs private
private is a TypeScript visibility modifier that exists only in the type system. #field is a JavaScript private field that the engine actually hides. They can be used together, but they are not the same feature.
Detailed Explanation
TypeScript had private years before JavaScript grew #fields. private email: string tells the compiler that other TypeScript should not read email. The emit is still this.email = .... In a debugger, in JSON.stringify, in a JS file next door, the property is visible.
#email is the language feature from the ECMAScript private-fields proposal. The name is not a string key on the object. It cannot be read with this["#email"]. It cannot be accessed from a subclass. It cannot be accessed from another class. in has a special form (#email in obj) that tests for the field without throwing. The engine enforces all of this.
TypeScript types # fields. You write #email: string and the compiler will error if you read #email outside the class body — which the syntax already forbids — and if you assign the wrong type inside it. Subclasses cannot see #email, so there is no protected equivalent for hash fields.
Choosing between them:
private x | #x | |
|---|---|---|
| Hidden from other TS? | yes | yes |
| Hidden from JS / runtime? | no | yes |
| Visible to subclasses? | no (protected exists) | no |
instanceof / structural typing | private members make classes nominal | hash fields also block structural assignability |
| Downlevel emit | ordinary property | WeakMap helper if target is old |
On old targets, TypeScript emits # fields as a WeakMap per field, which has a real cost and a slightly different in story. On modern targets, emit is native #.
A class can mix both. Use # for state that must not leak (tokens, passwords, internal caches). Use private for "please do not use this from other TS" when you are fine with the property existing, or when you need protected later. Do not mark the same conceptual field both ways.
Because # fields are not properties, they do not appear in Object.keys, spread, or for...in. Mappers that copy "all fields" will drop them, which is usually what you want.
Code Examples
1. TypeScript private is still a public property at runtime.
// 1. TypeScript private is still a public property at runtime.
class TsPrivate {
private token: string;
constructor(token: string) {
this.token = token;
}
}
const a = new TsPrivate("secret");
console.log((a as unknown as { token: string }).token); // "secret"2. Hash private is not a property.
// 2. Hash private is not a property.
class HashPrivate {
#token: string;
constructor(token: string) {
this.#token = token;
}
matches(other: HashPrivate): boolean {
return this.#token === other.#token; // same class, allowed
}
}
const b = new HashPrivate("secret");
// console.log(b.#token);
// Syntax error, not just a type error.
console.log("#token" in (b as object)); // false3. The `in` check for hash fields.
// 3. The `in` check for hash fields.
class Box {
#value: number;
constructor(value: number) {
this.#value = value;
}
static isBox(value: unknown): value is Box {
return typeof value === "object" && value !== null && #value in value;
}
}Common Gotchas
`#name` is a syntax error outside the class, not a type error you can `as` away
there is no legal cast that reads the field.
Old `target`s emit WeakMap helpers for `#` fields
that changes performance and can surprise code that expected a property. Raise `target` if the engine supports native private fields.
There is no `protected #field`
subclass access requires a `protected` TypeScript member or a protected method that reads the hash field.
Mixing `private token` and `#token` is two fields
they do not alias each other. Pick one per piece of state.