What are JSDoc @type curly braces for?

The JSDoc @type allows @type to specify the type of a variable, for example /** @type {Number} */ . However, I also see /** @type Number */ without curly braces, and it seems the same.

Use JSDoc has an example with and without curly braces, but does not discuss the difference. The Google Closure Compiler documentation implies that all ads must have curly braces, but do not indicate what will happen if they do not.

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

+6
source share
1 answer

My best guess is that curly braces exist for the parser, as some type specifications may have spaces in them.

eg. Object with string and numeric values:

 {Object.<string, number>} 

Depending on the parser, a simple type (such as a string) may not need curly braces, since the parser will consider the first space as the end of the type declaration.

edit: Further reading suggests that curly braces are required, so omitting them is a mistake, but some (if not most / all) parsers will forgive inaccurate type definitions if they can be processed as described above.

TL; DR: They are important, but you can leave without using them in some cases.

+4
source

Source: https://habr.com/ru/post/955459/


All Articles