Enum as @param type in JSDoc
39,152
Solution 1
You can achieve that, by doing this:
/**
* @param {(1|2)} type
*/
function useTypesEnum(type) {
}
Solution 2
So it seems this is the right way to document everything without any warning
/**
* @typedef {number} MyType
**/
/**
* @enum {MyType}
*/
var TYPES = {
TYPE_A: 1,
TYPE_B: 2
}
/**
* @param {MyType} type
*/
function useTypesEnum( type ) {
}
This means:
- MyType is a number
- TYPES is an enum that holds MyType values
- This function accepts enums that output MyType values
Works for me on intellij 2017.1
However - this will still allow each string to be passed to the function without warnings.
If you want to specify the enum values too - so it should raise errors if another string was used, use the method described at: https://stackoverflow.com/a/36501659/1068746
/**
* @typedef FieldType
* @property {string} Text "text"
* @property {string} Date "date"
* @property {string} DateTime "datetime"
* @property {string} Number "number"
* @property {string} Currency "currency"
* @property {string} CheckBox "checkbox"
* @property {string} ComboBox "combobox"
* @property {string} Dropdownlist "dropdownlist"
* @property {string} Label "label"
* @property {string} TextArea "textarea"
* @property {string} JsonEditor "jsoneditor"
* @property {string} NoteEditor "noteeditor"
* @property {string} ScriptEditor "scripteditor"
* @property {string} SqlEditor "sqleditor"
*/
Related videos on Youtube
![BuZZ-dEE](https://i.stack.imgur.com/xdlU1.jpg?s=256&g=1)
Author by
BuZZ-dEE
A free information society can only be based on free software and open standards. Contact: @buzz-dee:matrix.org using Element client
Updated on July 09, 2022Comments
-
BuZZ-dEE almost 2 years
Is it possible to use an enum for the JSDoc
@param
type declaration like in the following example?/** * @enum { Number } */ var TYPES = { TYPE_A: 1, TYPE_B: 2 } /** * @param { TYPES } type */ function useTypesEnum( type ) { }
If I use an IDE like Eclipse etc. for JavaScript, there should no warning be raised?
-
Xotic750 over 10 yearsDid you try? What happened?
-
BuZZ-dEE over 10 years
-
guy mograbi about 7 yearsDid you ever solve this?
-
-
xamiro over 8 yearsno, it doesnt create links to that enum, only to typedefs.
-
Ieuan almost 4 yearsThank you very much, this was what I was looking for and should be the accepted answer
-
Ahmed Mahmoud almost 4 yearsI couldn't tell you how you made my day, Leuan!
-
Jitsusama over 3 yearsI don't see how the requestor inferred that the comments would impact the code, instead, they wanted to know how to influence their IDE's type hint logic. What they seem to have requested was whether identifying enums in jsdoc was possible, and if so, what the syntax was, which your answer does not seem to help with. I will admit that their question could have been stated better though.
-
jakubiszon about 3 yearsCould that be combined with
@typedef
? -
Ahmed Mahmoud about 3 years@jakubiszon, yes. Just replace (1|2) with your custom type
-
jakubiszon about 3 years@AhmedMahmoud could I see an example of this? VSCode doesn't seem to pick it up when I do
/** @typedef {(1|2)} testEnum */
When I reference it, it understands the type istestEnum
but it doesn't suggest the values. -
Ahmed Mahmoud about 3 yearsIt works for me: imgur.com/a/yz9MHk7. Let me know if you tried the same and didn't work for you.
-
tehmas almost 3 yearsfor me this worked:
@param {'choice1'|'choice2'} type
-
Mark over 2 yearsThis solution results in
MyType
beingnumber
. This will ensure that you're sending something that is the same type as the enum's values, but it won't raise errors if you pass unknown values.useTypesEnum( 3 )
would not raise an error here, but should. -
Davide Vitali about 2 yearsWhat if, at some point, the underlying enumeration changes?