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

A protected constructor in TypeScript is a class constructor marked with the `protected` access modifier. It strictly confines direct instantiation (via the `new` operator) to the declaring class itself, while explicitly permitting derived classes to invoke it during their own initialization via `super()`. Derived classes cannot directly instantiate the base class using `new`; they are limited exclusively to the `super()` invocation.

```typescript theme={"dark"}
class Base {
    protected constructor(public readonly id: string) {
        // Initialization logic
    }

    static createInternal(): Base {
        // Allowed: Direct instantiation within the declaring class's lexical scope
        return new Base("internal_id"); 
    }
}

class ExplicitlyDerived extends Base {
    // Explicitly overrides the access modifier to public
    public constructor() {
        // Allowed: Invoking the protected base constructor from a subclass via super()
        super("derived_id"); 
    }

    static attemptDirectBaseInstantiation() {
        // Compiler Error ts(2674): Constructor of class 'Base' is protected and 
        // only accessible within the class declaration.
        // return new Base("invalid"); 
    }
}

class ImplicitlyDerived extends Base {
    // No explicit constructor. The implicitly generated constructor 
    // inherits the `protected` access modifier from Base.
}

// @ts-expect-error: Compiler Error ts(2674): Constructor of class 'Base' is protected...
const baseInstance = new Base("external_id"); 

// Allowed: The derived class explicitly exposes a public constructor
const derivedInstance = new ExplicitlyDerived(); 

// @ts-expect-error: Compiler Error ts(2674): Constructor of class 'ImplicitlyDerived' is protected...
const implicitInstance = new ImplicitlyDerived("implicit_id");
```

## Mechanical Behavior

* **Direct Instantiation Restriction:** When the `new` operator is applied directly to a class with a protected constructor from any scope outside the declaring class itself—including within derived classes—the TypeScript compiler emits error `ts(2674)`. Direct instantiation is exclusively confined to the declaring class.
* **Subclass Resolution:** Derived classes can successfully resolve the `super()` call during their own instantiation phase. If a derived class does not explicitly declare a constructor, TypeScript's implicitly generated constructor will automatically and legally invoke the protected base constructor. However, this implicitly generated constructor *inherits* the `protected` access modifier. Therefore, the derived class will also be uninstantiable from the outside unless it explicitly overrides the modifier by declaring a `public` constructor.
* **Internal Instantiation:** The base class retains the ability to instantiate itself directly (e.g., `new Base()`) from within its own static methods or static properties.
* **Contrast with Private Constructors:** A `private` constructor strictly confines both direct instantiation and invocation to the declaring class, completely prohibiting inheritance. TypeScript enforces this restriction at the declaration level by throwing an error directly on the `extends` clause (Error `ts(2675)`: `Cannot extend a class '...'. Class constructor is marked as private.`), preventing the subclass from even being declared. A `protected` constructor permits inheritance and subclassing while maintaining the restriction on direct construction.
* **Runtime Erasure:** Like all TypeScript access modifiers, the `protected` keyword is erased during compilation. At runtime, the emitted JavaScript constructor functions identically to a standard public constructor, meaning this restriction is enforced purely at compile-time by the TypeScript type checker.

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