【问题标题】:how jsdoc Classes with static propertiesjsdoc 类如何具有静态属性
【发布时间】:2020-03-30 05:57:36
【问题描述】:

我怎样才能在这里获得有效的设置?

我希望能够使用静态类 _Test.list 记录 id 属性 但我无法在 vscode 中找到使用智能感知的正确方法。 所以所有数字都不是来自_Test.list 字典,应该给我错误。

任何机构都可以帮助我用 jsdoc plz 正确格式化。 抱歉,如果是菜鸟问题,我从 jsdoc 开始。

class _Test {
    static list = { a:1,b:2,c:3 };
    constructor() {
        /**
        * @typedef {Object} DATA
        * @property {_Test.list} DATA.id - id from list _Test.list
        * @property {_Test.list} DATA.id2 - id from list _Test.list
        * 
        */
        /**@type {DATA} */
        this.list = {
            id: _Test.list.a, // should ok
            id2: 14, // should show a error
        }
    }
};

我想继续这样做是因为我需要在 vscode 中保留引用功能。

【问题讨论】:

  • 非常小的旁注:在类 declaration 之后不要使用;(这就是你上面的内容)。也不在构造函数定义之后(也不是方法)。但是您确实在赋值之后(在构造函数中)使用一个,除非您有意依赖 ASI。
  • 我把它放在示例中,认为它会更具可读性。对此感到抱歉。
  • @TJCrowder 是的,我创建了一个 typedef,我想将 DATA.id 链接到 _Test.list ,我无法正确引用该类的静态属性,也许我做得不好。跨度>
  • _Test.list[...] 在这种情况下是数字类型
  • 问题是如果我这样做 @property {Number} DATA.id 我可以分配任何数字例如 12 ,14,如果我分配一个不在静态字典内的数字 _Test.list[...] 我想要错误我用更多图片更新消息.

标签: javascript typescript visual-studio-code intellisense jsdoc


【解决方案1】:

JSDoc 不像 Typescript 那样有as const 的概念,至少在 VS Code 的 typescript 中是这样。但是你可以显式地给出一个文字类型:

/** @type {{ a: 1, b: 2, c: 3 }} */
static list = { a: 1, b: 2, c: 3 }

但首先定义允许的值并在索引签名中使用它们要简单得多:

/** @typedef {1 | 2 | 3} Values */
/** @typedef {{ [s: string]: Values }} DATA */

/** @type {DATA} */
static list = { a: 1, b: 2, c: 3 }

那么你也可以在其他地方使用DATA

class _Test {
    /** @type {DATA} */
    static list = { a:1,b:2,c:3 };
    constructor() {
        /** @type {DATA} */
        this.list = {
            id: _Test.list.a, // should ok
            id2: 14, // should show a error
        }
    }
};

【讨论】:

  • 哇,太棒了,非常感谢,我浪费了很多时间来尝试使用 vscode 进行工作@enum,您的解决方案工作。
【解决方案2】:

只想更新 现在智能支持 jsdoc 与 ts 混合 所以我们可以使用 @type {keyof staticList}

【讨论】:

    猜你喜欢
    • 2015-08-13
    • 1970-01-01
    • 1970-01-01
    • 2021-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多