首页 > 解决方案 > JSDoc如何在不创建新的typedef的情况下将成员添加到类型

问题描述

据说我有一种叫做 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 Object 的函数,这就是为什么

标签: sequelize.jsjsdoc

解决方案


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

/** 
 * @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
 */

推荐阅读