TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

JSDoc support for destructured parameters

Open kitsonk opened this issue 7 years ago • 11 comments

TypeScript Version: 2.0.3

Code

interface Foo {
    /**
     * A bar value
     */
    bar?: string;
}

/**
 * A function
 *
 * @param foo A param
 * @param { bar } Another param
 * @param bar Another param
 */
function foo(foo: string, { bar }: Foo): void {
    bar;
    foo;
}

foo('bar', { bar: 'play' });

Expected behavior:

Intellisense for the second argument, or the second argument properties.

Actual behavior:

No way of providing a description for the destructured elements of a destructured parameter.

destructured

kitsonk avatar Oct 26 '16 15:10 kitsonk

+1 As a JS developer relying on destructuring for function parameters, this issue generates a lot of false "problems" when mixing parameters with and without default values.

steph643 avatar Mar 23 '18 14:03 steph643

This does not work on version 1.21.1

/** Class representing a user. */
module.exports = class User {
  /**
  * Create a User.
  * @param {object} user
  * @param {string} user.id - The user id in uuid format
  * @param {string} user.name - The user name
  * @param {string} user.email - The user email
  * @param {string} user.password - The user password
  */
  constructor({id, name, email, password}) {
    this.id = id
    this.name = name
    this.email = email
    this.password = password
  }
}

eduardobcastro avatar Apr 04 '18 21:04 eduardobcastro

Also an issue when functions return an interface

interface Returns {
  /** This object contains a getter that returns itself */
  get: (key: string) => string;
}

const buildObject = (): Returns => ({
  get: key => key
})

// shows jsdoc description when hovering over "obj1.get"
const obj1 = buildObject();
const a = obj1.get("key")

// Only shows signature "const get: (key: string) => string"
const {get} = buildObject();
const b = get("key");

hellos3b avatar May 01 '20 20:05 hellos3b

Also would really like to have the comments made in my interface show on hover of destructured variables. In the mean time, has anybody found a workaround? I destructure code all over the place and can't figure out how to document the variables.

zachhardesty7 avatar Feb 03 '21 00:02 zachhardesty7

@edbentley I'm very interested in your last commit concerning this issue.

What did you mean with

Note we could move the jsdoc comments to this interface.

I'm experiencing the same issue where destructured function params don't show the JSDoc comment. : S

mesqueeb avatar Mar 09 '21 11:03 mesqueeb

@mesqueeb I just meant that it wasn't worth moving the existing JSDoc comments to the interface since they still won't show.

edbentley avatar Mar 10 '21 10:03 edbentley

Would like to see this implemented for the basic case:

interface Options {
  /**
   * description
   */
  param?: string;
}
const options: Options = {};
// `param` has no JSDOC
const { param }  = options

Any pointers where the implementation should happen (and tests)?

Should the description also be carried over if the destructured value has a default value e.g.

// Should `param` have the description of `options.param`?
const { param = 'someDefaultValue' }  = options

eps1lon avatar Apr 17 '21 09:04 eps1lon

Any pointers where the implementation should happen (and tests)?

Maybe https://github.com/microsoft/TypeScript/blob/master/src/services/jsDoc.ts ?

saschanaz avatar Apr 22 '21 02:04 saschanaz

Looking at this issue a year after subscribing to it, I feel the requirement doesn't make sense if we look at the implications of supporting this. Because if this is implemented then wouldn't it also mean carrying over descriptions at every assignment?

Since this

const { param }  = options;

is equivalent to

const param = options.param;

If the description is really important then maybe something like Nominal Types can be used so a reference to the original type is still around if not it's description, eg.:

image

TS Playground link

rohit-gohri avatar Apr 22 '21 04:04 rohit-gohri

This seems related to this weird issue I'm having with type inference on object destructuring. Posted about it here: https://github.com/microsoft/TypeScript/pull/44730#issuecomment-886865460

MarcosAtApprentice avatar Jul 26 '21 17:07 MarcosAtApprentice

Is JSDoc dead? This has not been fixed in almost 6 years.

PoulBak avatar Aug 08 '22 20:08 PoulBak

So effectively, JSDoc shall not be used to document TypeScript code. Fortunately, we can use markdown.

Eg. "### Example with ```ts"

A weird thing is, that "@link" can be used. "@param and @example" not.

steida avatar Nov 28 '23 10:11 steida