简体   繁体   English

JSDoc Type 属性为 function 参数无效

[英]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 .我正在开发一个 javascript 项目(使用 VSCode),我们使用JSDoc和 eslint 规则valid-jsdoc I have an object type which has some properties, as an example:我有一个 object 类型,它具有一些属性,例如:

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

In the code I am able to use the type Project['id'] ex:在代码中,我可以使用类型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.它识别类型Project['id']并在我将其悬停时指示它是一个string

I would like to force people to write JSDoc, so I want to use the eslint rule valid-jsdoc .我想强迫人们写 JSDoc,所以我想使用 eslint 规则valid-jsdoc However, the rule doesn't know the type Project['id'] and highlight the jsdoc with the error但是,该规则不知道类型Project['id']并突出显示带有错误的 jsdoc

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.我认为因为这个规则而不能使用Project['id']而使用string是很遗憾的,它不太明确,有时(对于更复杂的对象)可能会很麻烦。

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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