How to document a string type in jsdoc with limited possible values
Asked Answered
P

5

160

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;
};

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: Am using jsdoc3

Puny answered 30/9, 2013 at 12:10 Comment(4)
The multiple files problem makes this difficult. I usually see an enum for the definition and a union for the function parameter: ShapeType|string. However enums don't support adding subtypes after declaration in Closure-compiler.Desulphurize
@ChadKillingsworth I see what you mean. I am stuck at a point where I want to define a set of properties (lets say an object that goes as construction parameter of a class). It is well and good had all properties of the construction was defined at one location. Unfortunately, my code has a number of modules contributing to that construction properties. Doing something like a mixin or subclassing the propertied would be going overboard! As such, if I can simply inject to a property list definition it would be great.Puny
Another similar issue that I am facing, but with distributed property listing is #19114071Puny
All solutions below force us to create an Enum. There is an active feature request at GitHub to make this process much easier: github.com/jsdoc3/jsdoc/issues/629. So anybody who likes it should probably bump it.Clinch
C
35

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)

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.

Calvin answered 11/10, 2013 at 16:9 Comment(4)
Yes. The enumeration approach is the only usable way I see. Anyways, I accept this as the only usable answer - since the multi-file problem is another story altogether!Puny
The problem with this approach is that it does not allow to document the individual values. I have an issue with JSDoc. github.com/jsdoc3/jsdoc/issues/1065Ozan
I don't see how this can be ok. Parameter "a" is expected to be an object with two properties, but you provide a string to the function.Jehius
@LidelnKyoku - It works, because the type is declared to be an enum and then gets special treatment. Enumeration is not an object type but an enum type with string values. it does workCalvin
C
209

As of late 2014 in jsdoc3 you have the possibility to write:

/**
 * @param {('rect'|'circle'|'ellipse')} shapeType - The allowed type of the shape
 */
Shape.prototype.getType = function (shapeType) {
  return this.type;
};

Of course this will not be as reusable as a dedicated enum but in many cases a dummy enum is an overkill if it is only used by one function.

See also: https://github.com/jsdoc3/jsdoc/issues/629#issue-31314808

Clinch answered 12/1, 2016 at 13:19 Comment(1)
This is a better solution if you know that the param type will never change.Awlwort
D
76

What about:

/**
 * @typedef {"keyvalue" | "bar" | "timeseries" | "pie" | "table"} MetricFormat
 */

/**
 * @param format {MetricFormat}
 */
export function fetchMetric(format) {
    return fetch(`/matric}`, format);
}

enter image description here

Digiovanni answered 19/9, 2018 at 8:14 Comment(1)
Hey, is it possible to list the values from existing array? let say, I have array of string values, and I need to limit the input to the text that includes in this arrayFoch
C
35

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)

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.

Calvin answered 11/10, 2013 at 16:9 Comment(4)
Yes. The enumeration approach is the only usable way I see. Anyways, I accept this as the only usable answer - since the multi-file problem is another story altogether!Puny
The problem with this approach is that it does not allow to document the individual values. I have an issue with JSDoc. github.com/jsdoc3/jsdoc/issues/1065Ozan
I don't see how this can be ok. Parameter "a" is expected to be an object with two properties, but you provide a string to the function.Jehius
@LidelnKyoku - It works, because the type is declared to be an enum and then gets special treatment. Enumeration is not an object type but an enum type with string values. it does workCalvin
A
12

I don't think there's a formal way of writing allowed values in JSDoc.

You certainly can write something like @param {String('up'|'down'|'left'|'right')} like user b12toaster mentioned.

enter image description here

But, by taking reference from APIDocjs, here's what I use for writing constrained values, aka allowedValues.

/**
 * Set the arrow position of the tooltip
 * @param {String='up','down','left','right'} position pointer position
 */
setPosition(position='left'){
  // YOUR OWN CODE
}

Oh yeah, I'm using ES6.

Adhere answered 31/8, 2016 at 21:19 Comment(1)
@param does not allow this type of syntax. The most standard way would be @param {"up"|"down"|"left"|"right"} [position=left] - pointer position.Tripinnate
R
1

This is how the Closure Compiler supports it: you can use "@enum" to define a restricted type. You don't actually have to define the values in enum definition. For instance, I might define an "integer" type like:

/** @enum {number} */
var Int = {};

/** @return {Int} */
function toInt(val) {
  return /** @type {Int} */ (val|0);
}

Int is generally assignable to "number" (it is a number) but "number" is not assignable to "Int" without some coercion (a cast).

Reachmedown answered 30/9, 2013 at 23:35 Comment(4)
But that doesn't restrict the possible values of Int. That's the part I'm not sure is possible.Desulphurize
It does as much as any other type annotation or enum in JS. The restriction comes from how you write the code: every "cast" is a red flag. If you limit the casts to value factories then you get what you want: you can't assign 'number' to 'Int' without a warning.Reachmedown
It still doesn't restrict the values of {Int}. :-(Puny
Sure it does, you restrict the value of Int by limiting how it is created and the restriction is done when the value is created. A raw number can not be assigned which is all you need.Reachmedown

© 2022 - 2024 — McMap. All rights reserved.