JSDOC:如何定义类型的类型等于对象的属性名称 - 喜欢TypeScript的键?

发布于 2025-02-04 00:06:19 字数 487 浏览 3 评论 0原文

我正在寻找一种使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帝王念 2025-02-11 00:06:19
/*
* @typedef {{
* date: string,
* a1: string,
* a2: string,
* }} Record
*/

/** @type {Record['date']|Record['a1']|Record['a2']}*/
let fileds = 'a1';

请尝试使用此

-NEW编辑

如果您不想列出类型, 。也许您想尝试这种方式。

/** @typdef {string} RecordProperty */

/*
* @typedef {{
* date: RecordProperty,
* a1: RecordProperty,
* a2: RecordProperty,
* }} Record
*/

/** @type {RecordProprty} */
let fileds = 'a1';

/*
* @typedef {{
* date: string,
* a1: string,
* a2: string,
* }} Record
*/

/** @type {Record['date']|Record['a1']|Record['a2']}*/
let fileds = 'a1';

Try this

-New Edit-

If you don't want to list up the types. Maybe you want to try this way.

/** @typdef {string} RecordProperty */

/*
* @typedef {{
* date: RecordProperty,
* a1: RecordProperty,
* a2: RecordProperty,
* }} Record
*/

/** @type {RecordProprty} */
let fileds = 'a1';

┈┾☆殇 2025-02-11 00:06:19

您无需声明像枚举的对象,而是可以先声明元组(或在另一个@typedef中):

const OPTIONS = /** @type {const} */ (['one', 'two']);
/**
 * @type {typeof OPTIONS[number]}
 * @see OPTIONS
 */
const option = 'two';
// error TS2322: Type '"four"' is not assignable to type '"one" | "two"'.
// /** @type {typeof OPTIONS[number]} */ const invalidOption = 'four';

/**
 * @type {{[Key in OPTIONS[number]]: any}}
 */
const optionObj = {
  one: 1,
  two: 2,
  // error TS2322: Type '{ one: number; two: number; four: number; }' is not assignable to type '{ one: any; two: any; }'.
  // four: 4,
};

Instead of declaring a enum-like object, you can just declare the tuple first(or in another @typedef):

const OPTIONS = /** @type {const} */ (['one', 'two']);
/**
 * @type {typeof OPTIONS[number]}
 * @see OPTIONS
 */
const option = 'two';
// error TS2322: Type '"four"' is not assignable to type '"one" | "two"'.
// /** @type {typeof OPTIONS[number]} */ const invalidOption = 'four';

/**
 * @type {{[Key in OPTIONS[number]]: any}}
 */
const optionObj = {
  one: 1,
  two: 2,
  // error TS2322: Type '{ one: number; two: number; four: number; }' is not assignable to type '{ one: any; two: any; }'.
  // four: 4,
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文