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

A `protected` field in TypeScript is a class member access modifier that restricts the visibility and accessibility of a property or method strictly to the declaring class and any classes that derive from it (subclasses). It establishes a strict compile-time boundary that prevents external instance access while permitting internal inheritance chains.

```typescript theme={"dark"}
class BaseClass {
    protected internalState: number;

    constructor(value: number) {
        this.internalState = value;
    }
}

class DerivedClass extends BaseClass {
    public mutateState(): void {
        // Valid: Accessing a protected member from within a derived class
        this.internalState += 1; 
    }
}

const instance = new DerivedClass(10);

// Compile-time Error: Property 'internalState' is protected and only accessible 
// within class 'BaseClass' and its subclasses.
instance.internalState; 
```

## Technical Characteristics

**Lexical Visibility**
Access to a `protected` field is validated entirely at compile-time by the TypeScript compiler. The field cannot be referenced on an instantiated object from an external scope. It is resolvable only within the class body of the declaring class or its descendants, subject to specific cross-instance rules.

**Cross-Instance Access and Base Class Restrictions**
TypeScript permits cross-instance access of `protected` members, meaning a method can access the `protected` fields of another instance. However, a critical restriction applies within derived classes: a derived class can only access a `protected` member through an instance of its own type (or its subclasses). It cannot access the `protected` member through a reference typed explicitly as the base class.

```typescript theme={"dark"}
class Base {
    protected value: string;

    constructor(value: string) {
        this.value = value;
    }
}

class Derived extends Base {
    public compare(otherDerived: Derived, baseRef: Base): boolean {
        // Valid: Accessing protected field on another instance of the derived class
        if (this.value === otherDerived.value) {
            return true;
        }

        // Compile-time Error: Property 'value' is protected and only accessible 
        // through an instance of class 'Derived'. This is an instance of class 'Base'.
        return this.value === baseRef.value;
    }
}
```

**Compilation Erasure**
The `protected` modifier is a TypeScript-specific type-level construct. It does not emit any runtime enforcement mechanisms (such as JavaScript `#` private fields). During transpilation, the `protected` keyword is stripped, and the field is emitted as a standard, publicly accessible JavaScript property.

**Protected Constructors**
When the `protected` modifier is applied to a class `constructor`, it alters the instantiation rules of the class. The class cannot be instantiated directly using the `new` keyword from an external scope, but it can still be extended by derived classes.

```typescript theme={"dark"}
class NonInstantiableBase {
    protected constructor() {}
}

class ValidDerived extends NonInstantiableBase {
    constructor() {
        super(); // Valid: Subclass can invoke the protected constructor
    }
}

// Compile-time Error: Constructor of class 'NonInstantiableBase' is protected...
const base = new NonInstantiableBase(); 
```

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