jsdoc如何具有静态属性

问题描述 投票:2回答:1

我如何在这里获得有效的设置?enter image description here

我想使用静态类id记录_Test.list属性但无法在vscode中使用intellisense找到正确的方法。所以所有数字都不来自_Test.list字典,应该给我错误。enter image description here

任何人都可以帮助我使用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中保留引用功能。enter image description here

javascript typescript visual-studio-code intellisense jsdoc
1个回答
1
投票

JSDoc没有像Typescript那样具有as const的概念,至少在VS Code的打字稿中没有。但是您可以显式地指定文字类型:

/** @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
        }
    }
};
© www.soinside.com 2019 - 2024. All rights reserved.