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 private constructor in TypeScript is a class constructor explicitly declared with the private access modifier, which restricts the instantiation and subclassing of the class to its own lexical scope. By applying this modifier, the TypeScript compiler prevents the use of the new keyword on the class from any external context.
class RestrictedClass {
    private constructor() {
        // Initialization logic
    }
}

Technical Implications

1. Instantiation Restriction When a constructor is marked private, external attempts to create an instance yield a compile-time error. The class can only be instantiated from within its own static methods or static properties. 2. Inheritance Prevention A private constructor inherently makes a class final (un-extendable). Derived classes must invoke the base class constructor via super(). Because the base constructor is private, it is inaccessible to the derived class, resulting in a compilation failure. 3. Runtime Erasure Like all TypeScript access modifiers, the private keyword is a compile-time construct. It is erased during transpilation to JavaScript. At runtime, the constructor behaves as a standard JavaScript function, and the restriction is not enforced by the JavaScript engine.

Syntax and Behavior Visualization

class CoreSystem {
    // Constructor is hidden from external scopes
    private constructor(public readonly id: string) {}

    // Internal instantiation is permitted via static members
    public static initialize(): CoreSystem {
        return new CoreSystem("sys_01");
    }
}

// ❌ COMPILE ERROR: Constructor of class 'CoreSystem' is private and 
// only accessible within the class declaration.
const system = new CoreSystem("sys_02");

// ❌ COMPILE ERROR: Cannot extend a class with a private constructor.
class SubSystem extends CoreSystem {
    constructor() {
        super("sys_03"); // super() is inaccessible
    }
}

// ✅ VALID: Instantiation routed through an internal static method
const validSystem = CoreSystem.initialize();

Comparison with Protected Constructors

If a constructor is marked protected rather than private, external instantiation is still blocked, but the class can be extended. The private modifier is the strictest level of constructor encapsulation in TypeScript, blocking both external instantiation and inheritance simultaneously.
Master TypeScript with Deep Grasping Methodology!Learn More