Search code examples
visual-studio-codejsdocjsdoc3

`Rest parameters JSDOC` is not correctly flagged in vscode


Is there something wrong with my code that caused vscode to have no correct tip?

/**
 *
 * @param  {...Object} elementList
 * @param  {string} elementList[].type
 */
function add(...elementList) {
    // vscode tip: elementList is (parameter) elementList: any[]
    // expect: elementList is {type: string;}[]
    elementList.map(e => e)
}

// use
add({ type: 'div' }, { type: 'h1' })

Solution

  • You want the format @param {type} parameterName, e.g.

    /**
     * @param {{type: string}[]} elementList
     */
    function add(...elementList) {
        elementList.map(e => e)
    //  ^^^^^^^^^^^ (parameter) elementList: {type: string}[]
    }
    

    Alternatively, you can create re-usable type definitions with JSDoc:

    // Define an Object type named MyElement with one property (type) of type string.
    /**
     * @typedef {Object} MyElement
     * @property {string} type
     */
    
    /**
     * @param {MyElement[]} elementList
     */
    function add(...elementList) {
        elementList.map(e => e)
    //  ^^^^^^^^^^^ (parameter) elementList: MyElement[]
    }