javascript - 如何在对象属性中使用来自 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
*/
有人对此有什么建议吗?
解决方案
如果您在类型中有一个类型,则将您的枚举与汽车模型分开并在那里引用它。我认为这样你就可以使用 JSDoc 枚举。
const CarType = {
CarType1: 'CarType1',
CarType2: 'CarType2',
}
const Car = {
type: CarType
color: 'some string',
}
推荐阅读
- r - 使用 geom_sf 在 ggplot 中设置边界厚度和颜色主题
- excel - 如何在 Excel 的公式中只定义一次重复数组?
- html - 将主题和消息从 HTML 表单格式化为 mailto
- android - 华为地图套件,有没有办法在标记集群上定义 onClick 事件?
- python - 在 Python 中动态调用函数
- php - 如何用其他形式实现 dropzone 并将 post_id 插入图像表?
- cmake - 如何构建 CMake 项目?
- c - 分段错误 - 最长公共前缀分而治之
- sql - 在 T-SQL 中以领先或落后循环遍历表以创建订阅系列
- python - 类型错误:'在
' 需要字符串作为左操作数,而不是 re.Pattern