Skip to content

Intellisense for object properties defined in multi-line JSDoc comments #11597

Description

@abgivant

Currently it doesn't look like the TypeScript parser recognizes properties for an object parameter if they are defined with separate @param declarations. Would it be possible to add support for this?

image

image

Thanks!

Activity

  1. Jessidhia commented on Jan 6, 2017

    @Jessidhia

    A workaround is to write:

    /**
     * @param {{strProp: string, numProp: number}} obj - An object parameter with string and number properties
     */

    but that doesn't allow for documenting the specific subkeys.

  2. abgivant commented on Jan 6, 2017

    @abgivant
    Author

    @Kovensky Yeah, that does work, but the individual property documentation is the main reason I prefer the multi-line documenting.

  3. abgivant commented on Jan 16, 2017

    @abgivant
    Author

    Sorry, didn't mean to close/reopen this...

  4. ryansully commented on Mar 22, 2017

    @ryansully

    Would a solution to this also support destructured parameters as well?
    Example: jsdoc/jsdoc#987 (comment)

  5. abgivant commented on Mar 22, 2017

    @abgivant
    Author

    I haven't really used that pattern, but I would think it would. That seems to be an extension of the same underlying issue of being unable to document properties of object parameters.

  6. ankitsny commented on Apr 18, 2017

    @ankitsny

    How about creating a zouying (@typedef)
    this works fine for me.

    /**
     * @typedef {Object} GetUsersParams
     * @property {number} page_size          - Number of results to show in a page, optional?
     * @property {number} page_number        - Page number, optional?
     * @property {Object} match_params       - match query, optional?
     */
    /**
     * @description getUsers function can be used to fetch users
     * @param {GetUsersParams} params - Provide appropriate props
     * @param {function} cb
     */
    function getUsers(params, cb) {
        // do something
    }
    

    I tried this, but it doesn't work

    * @param {...GetUsersParams} params 
    
  7. abgivant commented on Apr 18, 2017

    @abgivant
    Author

    That's actually what I've been doing as a workaround, but it doesn't allow for documenting optional/default values.

    You also don't get the text descriptions in intellisense. I wouldn't say that's necessary, but it'd be nice.

  8. MikeyBurkman commented on May 17, 2017

    @MikeyBurkman

    Has there been any update on this? The @typedef workaround is not bad, but without the support of optional parameters (which seem to be supported for jsdocs), I'm getting a lot of false positives with jscheck turned on.

  9. mhegazy commented on May 17, 2017

    @mhegazy
    Contributor

    but without the support of optional parameters (which seem to be supported for jsdocs)

    can you elaborate on this?

  10. 3 remaining items

  11. zanza00 commented on Jul 11, 2017

    @zanza00

    I have a similar problem with stateless functional components in react, they are written with object destructuring and the trick with @typedef doesn't works because the parameter doesnt have a proper name

    I have tried with props, param0 , args0 but no luck 😞

    /**
     * @typedef Props
     * @property {string} one  - first 
     * @property {number} two  - second
     * 
     * @param {Props} props
     */
    
    // doesn't work
    function Component({one, two}) { 
    // stuff... 
    }
    
    // works
    function Component(props) { 
    const {one, two} = props
    // stuff... 
    }

    thanks for any advice 😄

  12. sandersn commented on Jul 18, 2017

    @sandersn
    Member

    Simone Picciani (@zanza00) you can you specify the type inline:

    function Component(/** @type {Props} */ { one, two }) {
     // stuff ...
    }
  13. zanza00 commented on Jul 18, 2017

    @zanza00

    I can confirm that indeed it works, thanks

  14. sandersn commented on Jul 21, 2017

    @sandersn
    Member

    Fix is up at #17352

  15. mohsen1 commented on Apr 8, 2018

    @mohsen1
    Contributor

    Nathan Shively-Sanders (@sandersn) It's still not possible to have a destructured parameter with @param annotations.

    Example:

     /**
      * @typedef BarBaz
      * @property {string} bar
      * @property {string} baz
      */
    
    /**
     * @param {BarBaz} options Your configuration object
     */
    function foo({bar, baz}) {
    
    }

    Error

     error TS8024: JSDoc '@param' tag has name 'options', but there is no parameter with that name.
    
    338  * @param {BarBaz} options Your configuration object
                           ~~~~~~~
    

    I can change my code to:

    function foo(/** @type {BarBaz} */{bar, baz}) {
    
    }

    but then where should I put the parameter description text?

    Also see webpack/webpack#6988

  16. sandersn commented on Apr 9, 2018

    @sandersn
    Member

    Mohsen Azimi (@mohsen1) that’s a slightly different issue than this one, and is tracked at #19645.

    The PR with the fix is at #18832. I’ll take another look at it; last I remember we needed to check whether performance was OK with the additional upfront processing and storage it required.

  17. sandersn commented on Apr 10, 2018

    @sandersn
    Member

    Mohsen Azimi (@mohsen1) destructured parameters now look up jsdoc @param tags positionally. It should be available tomorrow in typescript@next.

  18. Shahor commented on Apr 27, 2018

    @Shahor

    What do you mean by positionally?

    Are we heading towards something like

    /**
    * @param {number} $0.id An id
    */
    function foo({ id }) {}

    ?

  19. sandersn commented on Apr 27, 2018

    @sandersn
    Member

    Alexandre Gaudencio (@Shahor) no the syntax you would use is either

    /**
     * @param {Object} arg
     * @param {number} arg.id An id
     */
    function foo({ id }) {}

    OR

    /**
     * @param {{ id: number }} arg
     */
    function foo({ id }) {}
  20. steph643 commented on Jun 6, 2018

    @steph643

    Nathan Shively-Sanders (@sandersn), the two syntaxes you've suggested above doesn't seem to work. See here.

  21. sandersn commented on Jun 6, 2018

    @sandersn
    Member
  22. Shagon94 commented on Jun 14, 2018

    @Shagon94

    can confirm the issue, example code:

    /**
     * @param {*} options the args object
     * @param {number} object.alpha first number
     * @param {number} object.bravo second number
     * @param {Function} callback the callback function
     * @returns {number}
     */
    function addNumbersFromObject({ alpha = 1, bravo = 2 } = {}, callback = null) {
      if (!callback) return alpha + bravo;
      return callback(alpha + bravo);
    }
    
    console.log(addNumbersFromObject({ alpha: 2 }));

    that results in the text not being displayed:
    screen shot 2018-06-14 at 10 26 29 am

    The workaround is to use the @typedef like so:

    /**
     * @typedef {Object} Opts
     * @property {string} alpha Alpha helping test
     * @property {string=} bravo Description about bravo
     * 
     * @param {Opts} opts the options object
     * @param {Function} callback the callback function
     * @returns {number}
     */
    function addNumbersFromObject({ alpha = 1, bravo = 2 } = {}, callback = null) {
      if (!callback) return alpha + bravo;
      return callback(alpha + bravo);
    }
    
    console.log(addNumbersFromObject({ bravo: 2 }));

    Now intellisense detects the parameters as well as shows if its an optional parameter and adds description to it:
    screen shot 2018-06-14 at 10 29 07 am

  23. mhegazy commented on Jun 14, 2018

    @mhegazy
    Contributor

    Shagon94 it should be:

    /**
     * @param {Object} options the args object
     * @param {number} options.alpha first number
     * @param {number} options.bravo second number
     * @param {Function} callback the callback function
     * @returns {number}
     */
    function addNumbersFromObject({ alpha = 1, bravo = 2 } = {}, callback = null) {
        if (!callback) return alpha + bravo;
        return callback(alpha + bravo);
    }
    
    console.log(addNumbersFromObject({ alpha: 2 }));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Domain: JSDocRelates to JSDoc parsing and type generationSuggestionAn idea for TypeScriptVS Code TrackedThere is a VS Code equivalent to this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions