简体   繁体   English

VSCode JSDoc - 定义动态属性类型

[英]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 ).在我的“构造函数”中,我设置了一个父级用来进行 API 调用的字段,并在完成后将结果存储在data属性中(可通过this.data从我的类实例访问)。

How can I tell VSCode via JSDoc comments what the structure of this.data is?如何通过 JSDoc 注释告诉 VSCode this.data的结构是什么? I actually create the data structure that returns from the API call so it is known to me.我实际上创建了从 API 调用返回的数据结构,因此我知道它。 I have @typedef s in my code but I can't seem to properly tell the class what this.data looks like.我的代码中有@typedef s,但我似乎无法正确地告诉类this.data是什么样的。

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 .这种方法的问题在于它仅适用于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.在玩了一些之后,似乎最简单的事情就是在我的类“构造函数”(init 方法)中将this.data初始化为一个空字符串。 Above that initialization I used @type to define the structure of it.在初始化之上,我使用@type来定义它的结构。 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 = "";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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