我想使用静态类id
记录_Test.list
属性但无法在vscode中使用intellisense找到正确的方法。所以所有数字都不来自_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
}
}
};
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
}
}
};