Skip to content
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 spread operator #1074

Closed
robcolburn opened this issue Sep 24, 2015 · 13 comments
Closed

How to document spread operator #1074

robcolburn opened this issue Sep 24, 2015 · 13 comments

Comments

@robcolburn
Copy link

How do we document object spread operators in functions

/**
 * @param {object} cat - cute
 * @param {function} blender - pokey
 * @param {array} cubes - things to pass to blender
 * @return {string}
 *   result of blender
 */
const doThing = ( {cat, blender}, ...cubes ) => blender(cat, ...cubes)
@lifenautjoe
Copy link

+1

2 similar comments
@jtribble
Copy link

+1

@igor9silva
Copy link

+1

@brabeji
Copy link

brabeji commented Mar 29, 2016

Also please see #1163 , seems like code with spread operator can't be even parsed by jsdoc currently.

@ezzygemini
Copy link

ezzygemini commented Dec 13, 2016

+2

@igor9silva
Copy link

How is this solved nowadays?

@aliafshar
Copy link

aliafshar commented Dec 18, 2016

@param {...object} myParam

works for me

JSDoc 3.4.3 (Thu, 10 Nov 2016 00:25:10 GMT)

@hegemonic
Copy link
Contributor

See #987 and #1234 for examples of how to document this. I filed jsdoc/jsdoc.github.io#159 to add something about this to the docs.

@JanisE
Copy link

JanisE commented Oct 24, 2017

There is still no mention of the spread operator though, so I filed jsdoc/jsdoc.github.io#183 to add to the docs.

@axellbrendow
Copy link

axellbrendow commented Apr 8, 2020

I had a different need. I was doing my React component and I needed to document spread operator inside object destructuring. Seems that VSCode or JSDocs can not handle this properly.

I tried this way: (I've simplified)

/**
 * @typedef {{ value: number }} MyObj
 * @param {{ label?: string, rest: ...MyObj }} props
 */
const MyTextInput = ({ label, ...rest }) => {

But "rest" gets the weird type { rest: any, MyObj: any }. Yes, rest is considered as an object that has a property "rest" and another property "MyObj".

I also tried this way:

/**
 * @typedef {{ value: number }} MyObj
 * @param {{ label?: string, ...rest: MyObj }} props
 */
const MyTextInput = ({ label, ...rest }) => {

But "rest" gets the weird type { rest: MyObj }

So I found a solution that works for now:

/**
 * @typedef {{ value: number }} MyObj
 * @param {{ label?: string } & MyObj} props
 */
const MyTextInput = ({ label, ...rest }) => {

This time, "rest" type is correctly assigned to an object with the properties of MyObj.

@jimmywarting
Copy link

jimmywarting commented Jan 25, 2021

if i have something like this:

/**
 * @param {function} successCallback
 * @param {function} errorCallback
 * @param {SessionRequest=} sessionRequest
 */
function requestSession (successCallback, errorCallback, sessionRequest) {
  api.requestSession(successCallback, errorCallback, sessionRequest)
}

and i would want to just write

function request (...args) {
  api.requestSession(...args)
}

What would be the best way to write my jsdoc?

@raghav-misra
Copy link

if i have something like this:

/**
 * @param {function} successCallback
 * @param {function} errorCallback
 * @param {SessionRequest=} sessionRequest
 */
function requestSession (successCallback, errorCallback, sessionRequest) {
  api.requestSession(successCallback, errorCallback, sessionRequest)
}

and i would want to just write

function request (...args) {
  api.requestSession(...args)
}

What would be the best way to write my jsdoc?

Do correct me if I'm wrong but I think you could do:

/**
 * @param {function} successCallback
 * @param {function} errorCallback
 * @param {SessionRequest} sessionRequest
 */
function requestSession (...args) {
  api.requestSession(...args)
}

@tylim88
Copy link

tylim88 commented Feb 8, 2022

/**
 * @param {...number} args - array of number arguments
 */
export const a = (...args: number[]) => {
	return args
}

image

this will works, but the parameter name is fixed to args

indirect source: https://jsdoc.app/tags-type.html
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests