0
votes

We have a container object for our custom classes, but JSDoc doesn't seem to like my notation for these types and comments them with

"invalid type syntax".

I am using PHP-Storm.

My JSDoc is according to this principle:

/** @type {Container['3rd'].className} */

Screenshot of the comment

When I change the annotation to this it seems to be valid since the comment disappears:

/** @type {Container.3rd.className} */

Why is the first property accessor syntax not considered valid in JSDoc? The accessor using brackets is valid otherwise in Javascript code so why not in jsdoc annotations?

1
Just a guess but; no its not valid. The reason I'd guess this is because using bracket annotation is one way to avoid property renaming. - Graham P Heath
The syntax comment already suggests that it isn't valid. What I am interested in is why it is considered invalid. The accessor using brackets is valid otherwise in Javascript code so why not in jsdoc annotations? That it prevents property renaming (e.g. during minification or compiling) is a quality but that doesn't explain why it would be invalid syntax. - Wilt

1 Answers

2
votes

JSDocs @type notation is not a JavaScript expression. It is a JSDoc Namepath Expression.

If you look into these 2 links in detail you'll see a few things;

  • There are valid namepaths that would not be valid js:

    MyConstructor#instanceMember
    MyConstructor~innerMember
    
  • There are valid @type expressions that use non-JS syntax:

    /** @type {(string|Array)} */
    
  • There are no mentions of using Bracket notation in either of these docs (for types/namespaces).