【问题标题】:JSDoc type equal to member of arrayJSDoc 类型等于数组成员
【发布时间】:2014-05-21 12:23:53
【问题描述】:

假设我有一个字符串数组。

/** @type Array.<string> */
var possibleValues = ['a','b','c','d'];

我有一个接受字符串的函数,但我只希望它等于possibleValues 的成员之一。

/**
 * It does nothing.
 * @param {string} input One of the members of `possibleValues`.
 * @returns {string} Same as your input, useless... or is it?
 */
function nothing(input) { return input; }

JSDoc 3 中有没有办法可以将input 参数的类型显式设置为possibleValues 的值之一?

最好我想引用原始数组,而不仅仅是在评论中列出它的成员,因为我可能会重复使用它,并且以后可能会更改。

【问题讨论】:

  • 我根据您的措辞猜测该数组在运行时不会更改(否则我不希望您提及“列出其成员”,因为静态值列表会产生误导) ,但我不能 100% 确定我是对的。
  • @Louis 它在运行时不会发生变化,“可能会发生变化”我的意思是在应用程序的未来版本中。
  • 可能是@typedef标签(usejsdoc.org/tags-typedef.html)可以帮助你引用它。

标签: javascript jsdoc


【解决方案1】:

我会为此使用@enum

/**
 * @readonly
 * @enum {string}
 */
var possibleValues = {
    A: 'a',
    B: 'b',
    C: 'c',
    D: 'd'
};

/**
 * It does nothing.
 * @param {possibleValues} input Something descriptive...
 * @returns {string} Same as your input, useless... or is it?
 */
function nothing(input) { return input; }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-03
    相关资源
    最近更新 更多