How to use jsdoc when class in property, and when using Object.assign?

Viewed 112

My goal

  1. to add objects of defined type when creating objects
  2. methods are hidden in a namespace

I have two questions in the code below

  1. the type of result1 does not show the full type when using Object.assign
  2. the type of result2 always shows typeof Result, but I expect it to be the type of instance

Code explanation

Result will be created, when creating I need to add A and B_plus (possibly other extended classes) and will new an Object in other js files. There is no way to use module in this project.

If the above problems cannot be solved, what is a better way to achieve my goal?

const Namespace = {};

{
    class Result {}

    class A {
        a;
    }

    class B {
        b;
    }

    class B_Plus extends B {
        bplus;
    }

    class Builder {
        /**
         * @instance
         */
        result;

        constructor() {
            this.result = new Result();
        }

        /**
         * 
         * @param {A} a 
         */
        addA(a) {
            this.result = Object.assign(this.result, { a });
            return this;
        }

        /**
         * 
         * @param {B} b
         */
        addB(b) {
            this.result = Object.assign(this.result, { b });
            return this;
        }

        build() {
            return this.result;
        }
    }
    Namespace.Result = Result;
    Namespace.A = A;
    Namespace.B = B;
    Namespace.B_Plus = B_Plus;
    Namespace.Builder = Builder;

    /**
     * after build, I get {Result}.
     * this is the way I find to define the type
     * @type {Result & {a: A, b: B_Plus}}
     */
    let result1 = new Builder()
                    .addA(new A())
                    .addB(new B_Plus())
                    .build();
}

/**
 * dont know how to get correct type 
 * at least be {Result} of {Namespace.Result}
 * but I get {typeof Result}
 * @type {typeof Namespace.Result}
 */
let result2 = new Namespace.Builder()
                  .addA(new Namespace.A())
                  .addB(new Namespace.B_Plus())
                  .build();
console.log(result2);

1 Answers

The following code would do the trick. Use @template for the Builder and add methods. Then typecast some types to be compatible with /** @type {any}(...)

const Namespace = {};

{
    class Result {}

    class A {
        a;
    }

    class B {
        b;
    }

    class B_Plus extends B {
        bplus;
    }

    /**
     * @template {Result} T
     */
    class Builder {
        /**
         * @type {T}
         */
        result;

        constructor() {
            this.result = /** @type {any} */(new Result());
        }

        /**
         * @template {A} U
         * @param {U} a
         * @returns {Builder<T & U>}}
         */
        addA(a) {
            this.result = Object.assign(this.result, { a });
            return /** @type {any} */(this);
        }

        /**
         * @template {B} U
         * @param {U} b
         * @returns {Builder<T & U>}}
         */
        addB(b) {
            this.result = Object.assign(this.result, { b });
            return /** @type {any} */(this);
        }

        build() {
            return this.result;
        }
    }
    Namespace.Result = Result;
    Namespace.A = A;
    Namespace.B = B;
    Namespace.B_Plus = B_Plus;
    Namespace.Builder = Builder;

    let result1 = new Builder()
                    .addA(new A())
                    .addB(new B_Plus())
                    .build();
}

let result2 = new Namespace.Builder()
                  .addA(new Namespace.A())
                  .addB(new Namespace.B_Plus())
                  .build();
console.log(result2);

Screenshot

Related