Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are JSDoc @type curly braces for?

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?

like image 773
orlade Avatar asked Oct 08 '13 01:10

orlade


People also ask

What is JSDoc used for?

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.

What are the main purpose of curly brace?

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.

What does double curly braces mean?

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.

Should you use JSDoc with TypeScript?

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.


1 Answers

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.

like image 146
Tom Horwood Avatar answered Sep 24 '22 03:09

Tom Horwood