liveBook · Manning

Protected member

As explained in Essential TypeScript 5, Third Edition

An access control keyword specifies which code can access a class member or other program element.

Use `protected` when a subclass needs a class member but other code should not access it directly. A protected property or method is accessible within the class that defines it and within its subclasses. It sits between `private`, which permits access only within the defining class, and `public`, which permits free access and is the default. section 11.3.1

Let a persistent collection use inherited state

In `src/todoCollection.ts`, the base class declares `protected itemMap = new Map<number, TodoItem>();`. Its subclass, `JsonTodoCollection`, can then call `this.itemMap.set(...)` while loading tasks from `Todos.json`. Changing the map from private to protected makes that subclass access possible without making the map public. section 2.5

Listing 2.45 Changing access control in the todoCollection.ts file in the src folder
import { TodoItem } from "./todoItem.js";

type ItemCounts = {
    total: number,
    incomplete: number
}

export class TodoCollection {
    private nextId: number = 1;
    protected itemMap = new Map<number, TodoItem>();
    
    constructor(public userName: string, todoItems: TodoItem[] = []) {
        todoItems.forEach(item => this.itemMap.set(item.id, item));
    }
 
    // ...methods omitted for brevity...
}

The base-class listing shows `protected` on the map that holds todo items, alongside a `private` property that remains limited to the base class. section 2.5

The same choice applies to generic classes: `DataCollection<T>` declares `protected items: T[]`, allowing a derived `SearchableCollection<T>` to use the inherited array for its `find` method. An access control keyword can also go on a constructor parameter; TypeScript then creates and assigns the corresponding instance property. section 12.3.6 section 11.3.4

Protected access is a TypeScript compiler check, not a runtime security boundary. The generated JavaScript does not enforce it, so do not use `protected` to shield sensitive data from the rest of an application. section 11.3.1

How Essential TypeScript 5, Third Edition covers it

How deep it goes
Defines the access rule and applies it to collection properties, while warning that enforcement stops at compilation.
Examples
Shows a protected todo-item map used for JSON persistence and a protected array used by a searchable generic collection.
What it assumes
Assumes familiarity with classes and subclasses while defining what TypeScript's access control keywords permit.
What you can do after
Apply it via TypeScript to share class members with subclasses without declaring them public.
How it connects
Connects class inheritance, public and private access, generic classes, and concise class constructors.

Also explained in

Compare how 4 books explain protected 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.