Question

The JSDoc @type tag allows specification of a variable type, such as /** @type {Number} */. However I've also see /** @type Number */ without the curly braces, and it seems equally valid.

Use JSDoc has an example both with and without the curly braces, but doesn't discuss the difference. The Google Closure Compiler documentation implies that all declarations must have curly braces, but doesn't specify what happens if they don't.

Are the curly braces important? If so, why? And if not, should I use them or not?

Was it helpful?

Solution

My best guess is that the curly braces are there for the parser, as some type specs can have spaces in them.

eg. An object with string keys and number values:

{Object.<string, number>}

Depending on the parser a simple type (eg string) may not need the curly braces as the parser will read the first whitespace as the end of the type declaration.

edit: Further reading suggests that the curly brackets are required, so omitting them is an error, BUT some (if not most/all) parsers will forgive sloppy type definitions if they can be handled as above.

tldr: They are important, but you can get away without using them in some cases.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top