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.

An abstract getter in TypeScript is an accessor declaration within an abstract class that enforces a contract for a property’s retrieval without providing an implementation. It mandates that any concrete subclass must provide a specific implementation for that getter, ensuring strict type adherence and structural conformity across derived classes.

Syntax

An abstract getter is defined using the abstract modifier followed by the get keyword. It must terminate with a semicolon rather than a block statement.
abstract class BaseClass {
    abstract get propertyName(): Type;
}

Implementation Mechanics

When a concrete class extends an abstract class containing an abstract getter, the TypeScript compiler enforces the following rules:
  1. Context Restriction: Abstract getters can only exist inside classes marked with the abstract keyword.
  2. No Implementation Body: The declaration cannot contain curly braces {} or any return logic.
  3. Type Covariance: The return type of the implemented getter in the subclass must be assignable to the return type defined in the abstract base class.

Resolution Methods

TypeScript’s structural typing system allows a subclass to fulfill an abstract getter contract in multiple ways. An abstract getter simply enforces that a property is readable; it does not restrict the property from being writable, nor does it inherently represent a read-only state. Method 1: Concrete Getter The subclass implements a standard get accessor with a function body.
abstract class AbstractNode {
    abstract get nodeType(): string;
}

class ElementNode extends AbstractNode {
    private _type: string = "element";

    // Fulfilling the contract via a concrete getter
    get nodeType(): string {
        return this._type;
    }
}
Method 2: Standard Mutable Property Because an abstract getter only dictates that a value must be readable, a standard mutable property perfectly satisfies the structural contract.
class MutableNode extends AbstractNode {
    // Fulfilling the contract via a standard mutable property
    nodeType: string = "mutable";
}
Method 3: Readonly Property A readonly property also satisfies the contract, as it fulfills the requirement of being readable while explicitly preventing reassignment at the subclass level.
class TextNode extends AbstractNode {
    // Fulfilling the contract via a readonly property
    readonly nodeType: string = "text";
}

Interaction with Abstract Setters

If an abstract getter is paired with an abstract setter, the subclass must provide a mutable implementation. A readonly property will no longer satisfy the contract.
abstract class Configuration {
    abstract get threshold(): number;
    abstract set threshold(value: number);
}

class AppConfig extends Configuration {
    private _threshold: number = 10;

    get threshold(): number {
        return this._threshold;
    }

    set threshold(value: number) {
        this._threshold = value;
    }
}
Note: In TypeScript 4.3 and later, the type of the abstract setter’s parameter can be wider than the abstract getter’s return type, provided the getter’s type is assignable to the setter’s type.
Master TypeScript with Deep Grasping Methodology!Learn More