简体   繁体   中英

How to use Enum annotation from JSDoc in Object properties

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 ?

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',
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM