javascript JSDoc包含来自另一个@typedef对象的所有属性

gxwragnw  于 2023-11-15  发布在  Java
关注(0)|答案(4)|浏览(191)

如果我使用JSDoc创建两个对象,其中一个具有另一个的所有属性,我如何显示它?
例如,你会有:

/** 
 * @typdef Foo
 * @type {Object}
 * @property {string} bar
 * @property {string} baz
 */

/**
 * @typedef Foobar
 * @type {Object}
 * @property {float} value
 * @property {string} bar
 * @property {string} baz
 */

字符串
但是我不想写两次bar/baz,我想继承它。

7fhtutme

7fhtutme1#

我相信你会这么做...

/** Type definition for Foo.
 * @typedef {Object} Foo
 * @property {string} bar
 * @property {string} baz
 */

/** Type definition for Foobar.
 * @typedef {Foo} Foobar
 * @property {float} value
 */

字符串
注意Foobar是如何从Foo“继承”的。

k75qkfdt

k75qkfdt2#

你可以做一个简单的jsdoc插件来使它,这样你就可以在typedef上使用@augments/@extends。例如,这样的插件可以找到here(如下所示):

/**
 * Define a jsdoc plugin to update typedefs that use augments.
 */
exports.handlers = {
  /**
   * Modify typedefs that use augments (extends).  Add the base typedef's
   * properties to the augmented typedefs.
   */
  parseComplete: function (e) {
    var typedefs = {},
        augmentedTypedefs = {},
        numAugmented = 0;
    /* Make a dictionary of all known typedefs and a dictionary of augmented
     * typedefs */
    e.doclets.forEach(function (doclet) {
      if (doclet.kind === 'typedef') {
        typedefs[doclet.longname] = doclet;
        if (doclet.augments && doclet.augments.length) {
          augmentedTypedefs[doclet.longname] = doclet;
        }
      }
    });
    while (Object.keys(augmentedTypedefs).length !== numAugmented) {
      numAugmented = Object.keys(augmentedTypedefs).length;
      Object.keys(augmentedTypedefs).forEach(function (name) {
        var doclet = augmentedTypedefs[name];
        /* If this typedef is augmented by an augmented typedef, skip it for
         * now.  Ignore self references */
        if (doclet.augments.some(function (augmentName) {
          return augmentName !== name && augmentedTypedefs[augmentName];
        })) {
          return;
        }
        /* Ensure we have properties */
        doclet.properties = doclet.properties || [];
        /* Make a dictionary so we don't clobber known properties. */
        var properties = {};
        doclet.properties.forEach(function (prop) {
          properties[prop.name] = prop;
        });
        /* For each augment base, add its properties if we don't already have
         * them.  If the typedef augments two other typedefs that each have a
         * property of the same name, the last listed will be shown (done by
         * reversing the augments list). */
        doclet.augments.slice().reverse().forEach(function (augmentName) {
          if (augmentName !== name && typedefs[augmentName] && typedefs[augmentName].properties) {
            typedefs[augmentName].properties.forEach(function (prop) {
              if (!properties[prop.name]) {
                /* Make a copy so we don't mutate the original property. */
                prop = Object.assign(prop);
                /* Add a value that a rendering template could use to show that
                 * the property was inherted from a parent.  Since that in turn
                 * could have been inherited, preserve a known value. */
                prop.inherited = prop.inherited || augmentName;
                /* Add the property to the typedef and to the list of known
                 * properties. */
                doclet.properties.push(prop);
                properties[prop.name] = prop;
              }
            });
          }
        });
        /* We've finished processing this typedef, so remove it from the
         * augmented list. */
        delete augmentedTypedefs[name];
      });
    }
  }
};

字符串
在这一点上,你可以做:

/** 
 * @typdef Foo
 * @type {Object}
 * @property {string} bar
 * @property {string} baz
 */

/**
 * @typedef Foobar
 * @type {Object}
 * @augments Foo
 * @property {float} value
 */


如果需要,您可以修改模板,以不同方式显示继承的属性和非继承的属性(参见示例):

rjee0c15

rjee0c153#

这在VSCode中有效,不知道它有多理想。

/**
 * @typedef {Object} Person
 * @property {string} firstName
 * @property {string} lastName
 * @property {number} age
 */

/**
 * @typedef {Object} BaseUser
 * @property {string} username
 */

/**
 * @typedef {Person & BaseUser} User
 */

字符串
用户现在可以对PersonBaseUser的所有属性进行智能感知

xxhby3vn

xxhby3vn4#

我在解决一个类似的问题时发现了这样一个解决方案:

/**
 * @typedef { { id: number, name: string }} type_1
/** 

/**
 * @typedef { { value: number} } type_2
/** 

/** 
 * @typedef { type_1 & type_2 } combination_of_1_and_2
*/

字符串

相关问题