liveBook · Manning

Private member

As explained in Essential TypeScript 5, Third Edition

A private field is a JavaScript class data member marked with # whose access is enforced by the JavaScript runtime and restricted to its defining class.

A private member is a class field or method whose name begins with `#`. Only the class that defines it can use that name. Use a private field when class methods need data that other code should not access directly. The `#` remains in the JavaScript code, so the runtime enforces the restriction. section 11.3.2 section 4.2.7

Keep a department field inside its class

In `src/index.ts`, `Employee` declares `#dept: string`, assigns it with `this.#dept = dept` in the constructor, and reads it with `this.#dept` in `writeDept()`. Calling `salesEmployee.writeDept()` prints `Fidel Vega works in Sales`. The `#` is required at declaration and at each access. section 11.3.2

Listing 11.9 Using a private field in the index.ts file in the src folder
type Person = {
    id: string,
    name: string, 
    city: string
};

class Employee {
    public id: string; 
    public name: string;
    #dept: string;
    public city: string;
 
    constructor(id: string, name: string, dept: string, city: string) {
        this.id = id;
        this.name = name;
        this.#dept = dept;
        this.city = city;
    }
 
    writeDept() {
        console.log(`${this.name} works in ${this.#dept}`);
    }
}

let salesEmployee = new Employee("fvega", "Fidel Vega", "Sales", "Paris");

salesEmployee.writeDept();

The complete `Employee` listing shows the field declaration, constructor assignment, and method access together. section 11.3.2

Private methods use the same notation: a class can call `this.#getDetail()`, but attempting `boots.#getDetail()` outside it produces a syntax error. This differs from an underscore name such as `_price`, which merely signals that callers should leave a property alone; it does not prevent access. section 4.2.7 section 3.5.3

Do not confuse `#dept` with TypeScript’s `private dept`. Both restrict access in TypeScript class code, but the `private` keyword’s protection is enforced by the compiler and absent from generated JavaScript. Choose `#` when the restriction must also hold at runtime. section 11.3.1 section 11.3.2

How Essential TypeScript 5, Third Edition covers it

How deep it goes
Works through field declaration and access, contrasts compiler and runtime enforcement, and demonstrates an external-access syntax error.
Examples
Uses an `Employee` department field, a private tax-detail method, and an underscore-prefixed getter/setter backing property.
What it assumes
Assumes familiarity with class methods and constructors while demonstrating where the `#` goes in declarations and accesses.
What you can do after
Build it from scratch in a JavaScript or TypeScript class to restrict member access at runtime.
How it connects
Connects private fields to JavaScript classes, private methods, TypeScript access-control keywords, and getters and setters.

Also explained in

Compare how 12 books explain private member →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Essential TypeScript 5, Third Edition. The section links point to the parts of the book it draws on.