Search code examples
javascripteslintjsdoc

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


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!


Solution

  • 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
    };