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?
You can use most JSDoc type syntax and any TypeScript syntax, from the most basic like string to the most advanced, like conditional types.
Enums are one of the few features TypeScript has which is not a type-level extension of JavaScript. Enums allow a developer to define a set of named constants. Using enums can make it easier to document intent, or create a set of distinct cases. TypeScript provides both numeric and string-based enums.
Using TypeScript consistently offers a lot of benefits, especially for writing clean code to prevent unnecessary bugs and errors.
You can achieve that, by doing this:
/** * @param {(1|2)} type */ function useTypesEnum(type) { }
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:
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" */
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With