英文:
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.<string, MyEnum[]>} MyEnumObject
* @property {MyEnumObject} event
* @property {MyEnumObject} observation
*
* @type {{
* event: MyEnumObject,
* observation: MyEnumObject
* }}
*/
const enums = {
// Your object initialization here
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论