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

A `protected` method in TypeScript is a class member restricted by an access modifier that limits its visibility and invocation exclusively to the declaring class and any derived classes (subclasses) within the inheritance hierarchy. Unlike `public` methods, it cannot be accessed externally via class instances, and unlike `private` methods, its accessibility propagates down the prototype chain.

```typescript theme={"dark"}
class BaseClass {
    protected methodName(): void {
        // Method implementation
    }
}
```

**Core Mechanics and Constraints**

* **Internal Access:** The method can be invoked using the `this` or `super` keywords within the defining class and any class that `extends` it.
* **External Restriction:** Attempting to invoke a protected method on an instantiated object results in a `ts(2445)` compile-time error.
* **Visibility Widening:** When overriding a `protected` method in a derived class, TypeScript permits widening the access modifier to `public`. However, the compiler strictly prohibits narrowing the visibility to `private`.
* **Cross-Instance Access:** A class can access the protected methods of other instances of the *same* class type. However, a derived class cannot access protected members of a base class instance unless the reference is typed as the derived class.

**Type System Impact**
The `protected` modifier introduces *nominal* typing behavior into TypeScript's otherwise structural type system. Two classes with identical structural shapes are not type-compatible if their `protected` members do not originate from the exact same base declaration.

**Compilation and Runtime Behavior**
The `protected` keyword is strictly a compile-time construct that is erased during transpilation. In the emitted JavaScript, the method behaves identically to a standard prototype method and is fully accessible at runtime. There is no ECMAScript runtime equivalent for `protected` encapsulation (unlike ECMAScript `#` identifiers, which enforce strict runtime privacy but cannot be inherited or combined with TypeScript access modifiers).

**Syntax and Behavior Visualization**

```typescript theme={"dark"}
class Base {
    protected computeHash(): string {
        return "base_hash";
    }
}

class Derived extends Base {
    public execute(): void {
        // Valid: Accessing inherited protected method via 'this'
        const hash = this.computeHash(); 
    }

    // Valid: Overriding the method and widening visibility to 'public'
    public computeHash(): string {
        // Valid: Accessing base implementation via 'super'
        return super.computeHash() + "_derived"; 
    }
}

class InvalidDerived extends Base {
    // @ts-expect-error: Cannot narrow visibility from 'protected' to 'private'
    private computeHash(): string {
        return "invalid";
    }
}

const baseInstance = new Base();
// @ts-expect-error: Property 'computeHash' is protected and only accessible within class 'Base' and its subclasses.
baseInstance.computeHash(); 

const derivedInstance = new Derived();
derivedInstance.computeHash(); // Valid: Visibility was widened to 'public' in the Derived class.
```

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