0

I started to use more often the jsdocs and I searched around how to use the enum type and still a doubt about it.

Here are the usage definition at JSDocs: Enum definition at JSDocs The example showed is about a single enum object, how it would work if I have an Object which one specific field is an Enum type ?

Consider that Im using the sequelize orm and the definition it is about a Model.

For example.

/**
* @name Car
* @typedef {Object} Car - This is a car Model.
* @property {string} type - Enum type.
* @property {string} color - This is an attribute for car's color
*/
const Car = {
  // This should be considered as an enum type of strings.
  type: {
    type: ENUM,
    values: ['0', '1'],
    defaultValue: '0',
  },
  color: { 
    type: STRING,
    defaultValue: 'color',
  }
}

So, that way that I think that should work it would be like (which is not so fancy):

{ 
  ...
  /**
    * @enum
  */
  type: {
    type: ENUM,
    values: ['0', '1'],
    defaultValue: '0',
  },
  ...
}

I was wondering if have some option which works like:

/**
* @name Car
* @typedef {Object} Car - This is a car Model.
* @property {string} type - Enum type.
* @enum
* @default 'Car1'
* @property {string} color - This is an attribute for car's color
*/

Someone have some suggestion about it ?

  • https://stackoverflow.com/questions/287903/what-is-the-preferred-syntax-for-defining-enums-in-javascript – HDM91 Jul 05 '19 at 17:08

1 Answers1

1

If you have a type inside a type separate your enum from car model and reference it there. i think this way you can use JSDoc enum.

const CarType = {
 CarType1: 'CarType1',
 CarType2: 'CarType2',
}

const Car = {
  type: CarType
  color: 'some string',
}
HDM91
  • 1,318
  • 9
  • 16
  • Sorry I forgot to put some informations. But would you recommend to split into two objects, which the first one it would be considered an Enum type ? – Claudio Djohnnatha Jul 05 '19 at 17:12
  • 1
    Yes i think you should separate your enum type from your model and then reference that type in car model – HDM91 Jul 05 '19 at 17:28