JSDoc如何在不创建新typedef的情况下添加成员以进行输入

问题描述 投票:1回答:2

应该是我叫A和B的类型

/** 
 * @typedef A
 * @type {object}
 * @property {String} id this is id A
 * @property {String} name this is name A
 * @property {String} child_id this is id B
 */

/** 
 * @typedef B
 * @type {object}
 * @property {String} id this is id A
 * @property {Number} qty this is qty B
 */

反正我能有这样的事情

{
   id:"a",
   name:"a",
   B:{
     id: "b",
     qty:0
   }
}

不重写typedef?

我有一个返回Sequelize对象的函数,这就是为什么

sequelize.js jsdoc
2个回答
0
投票

从我对您的问题的阅读中看来,您可以通过修改A的typedef来解决此问题,给定您提供的预期输出对象。

/** 
 * @typedef A
 * @type {object}
 * @property {String} id this is id A
 * @property {String} name this is name A
 * @property {B} type B
 */

/** 
 * @typedef B
 * @type {object}
 * @property {String} id 
 * @property {Number} qty 
 */

除非您需要一个特定的字段来引用子B对象的ID?而你不希望明确定义为某种原因,孩子B关系。在这种情况下,您可以这样做并指定B为可选。

/** 
 * @typedef A
 * @type {object}
 * @property {String} id this is id A
 * @property {String} name this is name A
 * @property {String} idOfB - A string that holds the id from B
 * @property {B} [B] - Notice the square brackets, this makes it an optional property.
 */

这是当VSCode提供类型帮助时生成的JSDoc的样子

enter image description here


0
投票

最后我所做的是使用&关键字相交

/** 
 * @typedef A
 * @type {object}
 * @property {String} id this is id A
 * @property {String} name this is name A
 * @property {String} child_id this is id B
 */

/** 
 * @typedef B
 * @type {object}
 * @property {String} id this is id A
 * @property {Number} qty this is qty B
 */

我可以说我的定义

@returns {A & { B : B }} 

或创建新的typedef

/** 
 * @typedef C
 * @type {object}
 * @property {A & {B:B}} B inside A relation
 */
© www.soinside.com 2019 - 2024. All rights reserved.