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



