Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A protected getter in TypeScript is a class property accessor defined with the protected access modifier and the get keyword. It restricts the retrieval of a dynamically computed or encapsulated property value strictly to the defining class and its derived subclasses, preventing access from the public API of the class instances.
class BaseClass {
    private _internalValue: string = "core_data";

    // Protected getter definition
    protected get internalValue(): string {
        return this._internalValue;
    }
}

class DerivedClass extends BaseClass {
    public exposeValue(): string {
        // Valid: Accessing the protected getter within a subclass
        return this.internalValue.toUpperCase(); 
    }
}

const instance = new DerivedClass();

// Compiler Error: Property 'internalValue' is protected and only accessible 
// within class 'BaseClass' and its subclasses.
// console.log(instance.internalValue); 

Technical Mechanics

Compile-Time Enforcement The protected modifier is exclusively a TypeScript compile-time construct. It does not alter the emitted JavaScript. At runtime, the getter behaves as a standard prototype property defined via Object.defineProperty, and the access restriction is not natively enforced by the JavaScript engine. Type Inference and Annotations The return type of the get method dictates the type of the property. While TypeScript can infer this type from the return statement, explicitly annotating the return type is standard practice to ensure strict contract adherence. A getter cannot have parameters. Interaction with Setters If a protected get is declared without a corresponding set accessor, TypeScript automatically infers the property as readonly within the allowed scope. When pairing a protected getter with a setter, TypeScript strictly requires both accessors to share the exact same access modifier. Defining a protected get alongside a private set or public set will trigger compiler error TS2380: “A ‘get’ and ‘set’ accessor must have the same access modifier.” While TypeScript 4.3 introduced the ability for getters and setters to have different types (e.g., a getter returning a number while the setter accepts a number | string), their visibility must remain identical.
class AccessControl {
    private _count: number = 0;

    // Both accessors must share the 'protected' modifier
    protected get count(): number {
        return this._count;
    }

    // Valid: Visibility matches the getter. 
    // (TypeScript 4.3+ allows the setter type to be wider than the getter type)
    protected set count(value: number | string) {
        this._count = typeof value === "string" ? parseInt(value, 10) : value;
    }
}
Interface Implementation A protected getter cannot be used to satisfy a property requirement defined in a TypeScript interface. Interfaces only describe the public shape of an object; therefore, any property or accessor fulfilling an interface contract must be public.
Master TypeScript with Deep Grasping Methodology!Learn More