简体   繁体   中英

JSDoc Type property as function parameter is not valid

I am working on a javascript project (with VSCode) and we use JSDoc and the eslint rule valid-jsdoc . I have an object type which has some properties, as an example:

/**
 * @typedef {Object} Project
 * @property {string} id
 * @property {string} name
 */

In the code I am able to use the type Project['id'] ex:

/**
 * @param {Project['id']} projectId
 */
function myFunction(projectId) {}

It recognises the type Project['id'] and indicates it is a string when I hoover it.

I would like to force people to write JSDoc, so I want to use the eslint rule valid-jsdoc . However, the rule doesn't know the type Project['id'] and highlight the jsdoc with the error

JSDoc syntax error

It doesn't show any error if I use

/**
 * @param {Project} project
 */
function myFunction(project) {}

or

/**
 * @param {string} projectId
 */
function myFunction(projectId) {}

I think it is a pity to not be able to use Project['id'] and use string instead because of this rule, it is less explicite and sometimes (for more complex object) it might be troublesome.

Is there anyway to achieve what I am trying to do?

Try this

/**
 * @typedef {Object} Project - A new project
 * @property {String} id - The Project id
 * @property {String} name - The Project name
 */

You can now use as it as used below

/**
 * @param {Project} project
 */
function myFunction(project) { 
    var id = project.id // hover over this
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM