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?
JSDoc is a markup language used to annotate JavaScript source code files. Using comments containing JSDoc, programmers can add documentation describing the application programming interface of the code they're creating.
Different programming languages have various ways to delineate the start and end points of a programming structure, such as a loop, method or conditional statement. For example, Java and C++ are often referred to as curly brace languages because curly braces are used to define the start and end of a code block.
In react, since - { } - can be used to inject variables, double curly braces - {{ }} - are used to inject javascript objects within your JSX. Follow this answer to receive notifications. edited Feb 1, 2020 at 18:52.
These days, JSDoc type checking with TypeScript is extremely powerful. While not quite on par with TypeScript (not all TypeScript syntax is supported in JSDoc), the gap in functionality is pretty small. Today, it's a completely legitimate choice to build a JavaScript codebase with all the benefits of static typing.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With