首页 > 解决方案 > 如何在对象属性中使用来自 JSDoc 的枚举注解

问题描述

我开始更频繁地使用 jsdocs,并搜索了如何使用 enum 类型,但仍然对此表示怀疑。

以下是 JSDocs 中的用法定义: JSDocs 中的枚举定义 该示例是关于单个枚举对象,如果我有一个特定字段为枚举类型的对象,它将如何工作?

考虑一下我使用 sequelize orm 以及它关于模型的定义。

例如。

/**
* @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',
  }
}

所以,我认为应该工作的方式就像(这不是那么花哨):

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

我想知道是否有一些类似的选项:

/**
* @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
*/

有人对此有什么建议吗?

标签: javascriptsequelize.jscommentsjsdocjsdoc3

解决方案


如果您在类型中有一个类型,则将您的枚举与汽车模型分开并在那里引用它。我认为这样你就可以使用 JSDoc 枚举。

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

const Car = {
  type: CarType
  color: 'some string',
}

推荐阅读