简体   繁体   中英

VSCode JSDoc - define dynamic property type

I have a class that extends a class from some external code. In my "constructor", I set a field that the parent uses to make an API call and upon completion, the result gets stored in the data property (accessible from my class instance via this.data ).

How can I tell VSCode via JSDoc comments what the structure of this.data is? I actually create the data structure that returns from the API call so it is known to me. I have @typedef s in my code but I can't seem to properly tell the class what this.data looks like.

The best I have been able to do that works is like the below:

class a extends b {
    render() {
        const stuff = /** @type {Stuff} */ (this.data).stuff[1].thing;
    }
}

The problem with this approach is that it is only applicable for that particular usage of this.data .

After playing around some, it seems the easiest thing to do was to just initialize this.data within my classes "constructor" (init method) to an empty string. Above that initialization I used @type to define the structure of it. I really didn't want to have to add erroneous code to achieve this but I guess it isn't too much.

/** @type {{FOCUS_NOTES: FOCUS_NOTES}} */
this.data = "";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM