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 parameterized constructor in TypeScript is a special constructor method within a class that accepts one or more typed arguments. It is invoked automatically during object instantiation via the new keyword, assigning the passed arguments to class properties to establish the initial state of the object.

Standard Syntax

In its most basic form, parameters are explicitly typed in the constructor signature, and their values are manually assigned to declared class properties using the this context.
class Entity {
    name: string;
    version: number;

    constructor(name: string, version: number) {
        this.name = name;
        this.version = version;
    }
}

Parameter Properties (Shorthand)

TypeScript provides a syntactic shorthand known as parameter properties. By prefixing a constructor parameter with an access modifier (public, private, protected) or the readonly modifier, TypeScript automatically declares the class property and initializes it with the provided argument. This eliminates the need for explicit property declarations and manual assignment.
class Entity {
    constructor(
        public name: string, 
        private version: number, 
        readonly id: string
    ) {
        // Properties are implicitly declared and assigned.
    }
}

Optional and Default Parameters

Constructor parameters follow standard TypeScript function arity rules. They can be marked as optional using the ? token or assigned default values. Optional parameters must follow all required parameters in the signature.
class NetworkConfig {
    constructor(
        public host: string, 
        public port: number = 8080, // Default parameter
        public timeout?: number     // Optional parameter
    ) {}
}

Constructor Overloading

TypeScript supports constructor overloading, allowing a class to define multiple constructor signatures to accept different parameter types or counts. However, there can only be one implementation signature. The implementation signature must be compatible with all overload signatures and is the only one that contains the actual execution logic.
class Coordinates {
    x: number;
    y: number;

    // Overload signatures
    constructor(x: number, y: number);
    constructor(xyString: string);

    // Implementation signature
    constructor(xOrString: number | string, y?: number) {
        if (typeof xOrString === "string") {
            const [parsedX, parsedY] = xOrString.split(",").map(Number);
            this.x = parsedX;
            this.y = parsedY;
        } else {
            this.x = xOrString;
            this.y = y!;
        }
    }
}

Inheritance and super()

When a derived class implements a parameterized constructor, it must invoke the base class’s constructor using the super() method. The arguments passed to super() must satisfy the parameter requirements of the base class constructor. Furthermore, super() must be executed before any reference to this is made within the derived constructor.
class BaseNode {
    constructor(public nodeId: string) {}
}

class ComputeNode extends BaseNode {
    constructor(nodeId: string, public cores: number) {
        super(nodeId); // Invokes BaseNode constructor
        // 'this' can only be accessed after super()
    }
}
Master TypeScript with Deep Grasping Methodology!Learn More