JSDOC:如何定义类型的类型等于对象的属性名称 - 喜欢TypeScript的键?
我正在寻找一种使用JSDOC将对象的属性名称作为类型的方法。
让一个名为记录
的对象的类型
/**
* @typedef {{
date: string,
a1: string,
a2: string,
}} Record
*/
我希望将变量字段
记录为等于对象的属性 - 对于此示例,这意味着:'日期'| 'a1'| 'a2'
/**
* @type {*keyof Record??*} in this case, this will be equal to @type {'date' | 'a1' | 'a2'}
*/
let fields = 'a1';
typeScript提供键关键字,这就是这样做的。不幸的是,我正在处理的系统不支持打字稿:\
I'm looking for a way to get an object's property names as a type using JSDoc.
Let there be a typedef of an object named Record
/**
* @typedef {{
date: string,
a1: string,
a2: string,
}} Record
*/
I want the variable fields
to be documented as equal to the object's properties- which means for this example: 'date' | 'a1' | 'a2'
/**
* @type {*keyof Record??*} in this case, this will be equal to @type {'date' | 'a1' | 'a2'}
*/
let fields = 'a1';
Typescript provides the keyof keyword, which does just that. unfortunately, the system I'm working on does not support typescript :\
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请尝试使用此
-NEW编辑
如果您不想列出类型, 。也许您想尝试这种方式。
Try this
-New Edit-
If you don't want to list up the types. Maybe you want to try this way.
您无需声明像枚举的对象,而是可以先声明元组(或在另一个
@typedef
中):Instead of declaring a enum-like object, you can just declare the tuple first(or in another
@typedef
):