JSDoc类型中的语法错误,当键名为 “event” 时

huangapple go评论62阅读模式
英文:

Syntax error in my JSDoc type when key is named "event"

问题

以下是已翻译的内容:

这是一个 JSDoc,eslint 的 JSDoc 插件不喜欢它:

@property {{event: Object.<string, MyEnum[]>, observation: Object.<string, MyEnum[]>}} enums

它报错说:类型中的语法错误:(...) jsdoc/valid-types

如果我将 "event" 重命名为 "events",它就可以工作了。

为什么我不能使用一个名为 "event" 的对象键呢?

已解决(由 Andrew Arrow 解决,请参见他的解决方案以下):

感谢 @andrew arrow,我最终将我的 "enums" 键分解如下,不再报错:

@property {Object} enums
@property {Object.<string, MyEnum[]>} enums.event
@property {Object.<string, MyEnum[]>} enums.observation

感谢 Andrew!

英文:

Here is a jsdoc that the jsdoc plugin of eslint does not like:

@property {{event: Object.<string, MyEnum[]>, observation: Object.<string, MyEnum[]>}} enums

It complains: Syntax error in type: (...) jsdoc/valid-types

If I rename "event" to "events", it works again.

Any idea why I am not allowed to have an object key named "event"?

SOLVED (by andrew arrow, see his solution below):

Thanks to @andrew arrow, I ended up decomposing my "enums" key like this, and it did not complain anymore:

@property {Object} enums
@property {Object.<string, MyEnum[]>} enums.event
@property {Object.<string, MyEnum[]>} enums.observation

Thanks Andrew!

答案1

得分: 2

问题特定于JSDoc注释和ESLint JSDoc插件。插件可能错误地将“event”解释为其他内容,从而触发“类型中的语法错误”投诉!

尝试使用JSDoc注释的替代语法,看看是否没有错误。不要使用内联类型标记属性,而是使用单独的@typedef为MyEnum的@type标记:

/**
 * @typedef {Object.<string, MyEnum[]>} MyEnumObject
 * @property {MyEnumObject} event
 * @property {MyEnumObject} observation
 *
 * @type {{
 *   event: MyEnumObject,
 *   observation: MyEnumObject
 * }}
 */
const enums = {
  // 在这里进行对象初始化
};
英文:

the problem is specific to the JSDoc annotation and the ESLint JSDoc plugin. The plugin might be mistakenly interpreting "event" as something else, triggering the "Syntax error in type" complaint!

Try an alternative syntax for the JSDoc annotation to see if it works without any errors. Instead of using the inline type notation for the property use the @type tag with a separate @typedef for MyEnum:

/**
 * @typedef {Object.&lt;string, MyEnum[]&gt;} MyEnumObject
 * @property {MyEnumObject} event
 * @property {MyEnumObject} observation
 *
 * @type {{
 *   event: MyEnumObject,
 *   observation: MyEnumObject
 * }}
 */
const enums = {
  // Your object initialization here
};

huangapple
  • 本文由 发表于 2023年7月20日 18:09:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76728802.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定