• Get a static property from a class instance.

    Type Parameters

    • T extends object

    Parameters

    • instance: T

      The instance of the class.

    • key: string

      The name of the static property to retrieve.

    Returns any

    The value of the static property.

    This function retrieves a static property from the class of the given instance. It traverses the prototype chain to find the property in the class constructor. If the property is not found, it throws an error.

    Error if the property is not found.

    class MyClass {
    static myStaticProperty = "Hello, world!";
    static get myProperty() {
    return this.myStaticProperty;
    }
    }
    const instance = new MyClass();
    const value = getStatic(instance, "myStaticProperty");
    console.log(value); // "Hello, world!"
    const method = getStatic(instance, "myProperty");
    console.log(method); // "Hello, world!"
    const notFound = getStatic(instance, "nonExistent"); // Error: Static property "nonExistent" not found in prototype chain.