> ## 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.

# TypeScript Protected Getter

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.

```typescript theme={"dark"}
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.

```typescript theme={"dark"}
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`.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor TypeScript Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
