-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to document array elements? #1073
Comments
Same question for me. |
I'm also wondering this. In my case, I have a typedef of an array, for which the first element has a particular significance and should always be a number. Any suggestions how to document this? |
i'm using it
|
/**
* The `@returns` below was autogenerated by WebStorm:
* @returns {Promise.<{f: [number,number], b: [string,string]}>}
*/
foo() {
return Promise.resolve({
f: [1, 2],
b: ['bar', 'baz']
})
} Is this correct (but under-documented) JSDoc? |
There's currently no way to document the expected type of individual array elements. The best you can do is the workarounds proposed by @brunano21 and @ElfenLiedGH. Or, of course, you can just use text: /**
* Foo function.
*
* @param {Array} bar - The bar array. The first element must be a number, and the
* second element must be a string.
*/
function foo(bar) {} I'm open to a pull request that supports something like the following example (inspired by WebStorm), but keep in mind that most of the work will involve modifying the Catharsis type-expression parser: /**
* Foo function.
*
* @param {[number, string]} bar - The bar array.
*/
function foo(bar) {} |
Just wanted to probe this issue again. Has any progress been made on this issue? For my two cents, the format generated (an presumably supported, though I haven't checked) by WebStorm makes a lot of sense to me, especially in return statements:
|
JSDoc syntax errors were causing "npm run docs" to fail. - JSDoc doesn't yet support specifying array content, i.e. "[number, number]" is a syntax error, so I'm using "number[]" instead. (source: jsdoc/jsdoc#1073) - JSDoc doesn't support multiline objects, e.g. returning report information was a syntax error, so I extracted it into a typedef, as well as the disable directive.
While it is not great DRY, one advantage of the /**
* @property {string} 0 type The type that...
* @property {string} 1 value The value which ...
*/ And if the list is long, the numeric indexes also do help one see at a glance what the number is without having to count commas.... |
Any updates on this from the maintainers? |
What if the array is defined like
or
|
@brunoenribeiro what exact values are expected to be passed in both of your examples? It's not immediately clear what a named element in an array is. |
Hey there! Sorry for the delay. In both cases, you should pass an array with 2 elements. For instance: There'll be no difference to the code itself. The only difference is how much information will be provided in the documentation. Other example: suppose you have a function In your docs, you could declare only that there'll be 3 numbers... ... but you could name each element and make it much clearer: But you still can destructure the array using any names you want: |
@brunoenribeiro , @zerkms : VisualStudio is using TypeScript's own extension of JSDoc. JSDoc itself does not appear to support the "tuples" you are describing; see #1703 nor do its docs embrace such a type (and Closure, whose types JSdoc mentions supporting, doesn't either: google/closure-compiler#379 ). (Side: note: these "tuples" are not the same as those in the stage 2 JS proposal: https://github.com/tc39/proposal-record-tuple ). Of course, it's good to know about this for those who are using the TypeScript extension, and it'd be nice for it to be supported. |
I would like to know the answer to this as well... |
This way
is throwing an error in the last versions of the library. |
I opened a PR to finally fix this, if you care enough, please test and feedback. This issue is open way too long by now: It supports tuples as they are in TypeScript (unnamed + named tuples). Even tho |
Hi,
I have a function that takes an array of strings.
Something like:
But I want to specify what each element within the array represents.
How can I achieve that? I tried with something like:
But obviously this is wrong since they are interpreted as
properties
.Of course, I also tried with
args[0]
but I get an error at jsdoc generation phase.Finally, Google did not help this time. :/
Any suggestion?
The text was updated successfully, but these errors were encountered: