[英]How to extend a typedef parameters in JSDOC?
我有一個 typedef 的 JSDoc 注釋,如下所示,
/**
* @typedef {Object} obj1
* @property {boolean} a - Property 1
* @property {boolean} b - Property 2
*/
我想要一個新的 typedef,它還包括以下屬性:
* @property {boolean} c - Property 3
* @property {boolean} d - Property 4
除了a和b之外,如何向 object 添加任何其他屬性?
在代碼中,它是這樣的:
const obj1 = {
a: true,
b: false
}
const obj2 = {
a: true,
b: false,
c: true,
d: false
}
如您所見, a和b是共享的,所以我不想重復定義它們,它們完全相同。
如何向現有定義添加屬性?
在代碼中,我們可以這樣做:
const obj2 = {
...obj1,
c: true,
d: false
}
我們可以做類似下面的 jsDoc 的事情嗎?
/**
* @typedef {Object} obj1
* @property {boolean} a - Property 1
* @property {boolean} b - Property 2
*/
/**
* @typedef {Object} obj2
* @property {...obj1}
* @property {boolean} c - Property 3
* @property {boolean} d - Property 4
*/
??
解決方法很簡單:
它是通過使用這種語法:
/**
* @typedef {Obj1Props & Obj2Props} obj2
*/
/**
* @typedef {Object} Obj1Props
* @property {boolean} a - Property 1
* @property {boolean} b - Property 2
*/
/**
* @typedef {Object} Obj2Props
* @property {boolean} c - Property 3
* @property {boolean} d - Property 4
*/
/**
* @typedef {Obj1Props & Obj2Props} obj2
*/
/** @type {Obj1Props} */
const obj1 = {
a: true,
b: false
}
/** @type {obj2} */
const obj2 = {
a: true,
b: false,
c: true,
d: false
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.