如何用有限的可能值在jsdoc中记录字符串类型 [英] How to document a string type in jsdoc with limited possible values

查看:83
本文介绍了如何用有限的可能值在jsdoc中记录字符串类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个接受一个字符串参数的函数.此参数只能具有几个定义的可能值之一.记录相同内容的最佳方法是什么?应该将shapeType定义为enum还是TypeDef或其他名称?

I am having a function that accepts one string parameter. This parameter can have only one of a few defined possible values. What is the best way to document the same? Should shapeType be defined as enum or TypeDef or something else?

Shape.prototype.create = function (shapeType) {
    // shapeType can be "rect", "circle" or "ellipse"...
    this.type = shapeType;
};

Shape.prototype.getType = function (shapeType) {
    // shapeType can be "rect", "circle" or "ellipse"...
    return this.type;
};

问题的第二部分是shapeType的可能值在将shapeType定义为您建议的文件中未知.有几个开发人员提供的多个文件可能会添加到shapeType的可能值.

The second part of the problem is that the possible values of shapeType is not known in the file that defines shapeType as whatever you suggest. There are multiple files contributed by several developers who might add to the possible values of shapeType.

PS:我正在使用jsdoc3

推荐答案

如何声明一个虚拟枚举:

How about declaring a dummy enum:

/**
 * Enum string values.
 * @enum {string}
 */
Enumeration = {
    ONE: "The number one",
    TWO: "A second number"
};

/**
 * Sample.
 * @param {Enumeration} a one of the enumeration values.
 */
Bar.prototype.sample = function(a) {};


b = new Bar();

bar.sample(Enumeration.ONE)

尽管如此,您至少需要向JSDOC声明枚举.但是代码很干净,您可以在WebStorm中自动完成.

You need to at least declare the enum to JSDOC, for this, though. But the code is clean and you get auto-completion in WebStorm.

尽管无法通过这种方式解决多文件问题.

The multiple files problem though cannot be solved this way.

这篇关于如何用有限的可能值在jsdoc中记录字符串类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆