dud*_*ude 21 javascript typedef extend jsdoc type-definition
假设您在ES6类(文档)中有以下代码:
/**
* @typedef Test~options
* @type {object.<string>}
* @property {array} elements - An array containing elements
* @property {number} length - The array length
*/
/**
* @param {Test~options} opt - Option object
*/
test(opt){
}
Run Code Online (Sandbox Code Playgroud)
现在我想记录另一个函数,让我们给它命名test2.此函数采用完全相同的options对象,但需要另一个属性parent.
如何记录这一点而不记录冗余选项?冗余意味着:
/**
* @typedef Test~options
* @type {object.<string>}
* @property {array} elements - An array containing elements
* @property {number} length - The array length
*/
/**
* @param {Test~options} opt - Option object
*/
test(opt){
}
/**
* @typedef Test~options2
* @type {object.<string>}
* @property {array} elements - An array containing elements
* @property {number} length - The array length
* @property {object} parent - The parent element
*/
/**
* @param {Test~options2} opt - Option object
*/
test2(opt){
}
Run Code Online (Sandbox Code Playgroud)
Aji*_*mar 21
我找到了这个解决方案,这对我来说非常好。最初来自这里
/**
* @typedef {Object} ChildType
* @property {String} childProp
*
* @typedef {Base & ChildType} Child
*/
Run Code Online (Sandbox Code Playgroud)
lam*_*345 12
我不喜欢没有“完整设置”的半生不熟的答案,所以这里是一个:
/**
* @typedef {Object} Person
* @property {string} name - The person's name
* @property {number} age - The person's age
*/
/**
* @typedef {Object} WizardProperties
* @property {string[]} magicPowers
* @typedef {Person & WizardProperties} Wizard
*/
/** @type {Wizard} */
export const wizard = {
name: "Harry",
age: 20,
magicPowers: ["brroooomm"]
}
Run Code Online (Sandbox Code Playgroud)
它也适用于多个文件,但您必须使用以下import('./person').Person样式:
/**
* @typedef {Object} Person
* @property {string} name - The person's name
* @property {number} age - The person's age
*/
Run Code Online (Sandbox Code Playgroud)
/**
* @typedef {Object} WizardProperties
* @property {string[]} magicPowers
* @typedef {import('./person').Person & WizardProperties} Wizard
*/
/** @type {Wizard} */
export const wizard = {
name: "Harry",
age: 20,
magicPowers: ["brroooomm", "wheeeeeeeeeeeew"]
}
Run Code Online (Sandbox Code Playgroud)
小智 7
试试吧
/**
* @typedef {object.<string>} Test~options
* @property {array} elements - An array containing elements
* @property {number} length - The array length
*/
/**
* @param {Test~options} opt - Option object
*/
test(opt){
}
/**
* @typedef {Test~options} Test~options2
* @property {object} parent - The parent element
*/
/**
* @param {Test~options2} opt - Option object
*/
test2(opt){
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4887 次 |
| 最近记录: |