JSDoc中对象中任意键的值的文档结构 [英] Document structure of a value for arbitrary keys in an Object in JSDoc

查看:135
本文介绍了JSDoc中对象中任意键的值的文档结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数 factory

function factory(events) {
  for(const key in events) {
    const { before, after } = events[key]
  }
}

其中参数 events 通常是:

{
  only: {
    before(){}
    after(){}
  },
  except: {
    before(){}
    after(){}
  },
}

其中键仅 除外可以是任何值,但值始终(必须是)为 {before,after} ,其中两个 before ,是函数。

Where the keys only, except can be anything but the values are always (must be) of type {before, after} where both before, after are functions.

如何在我的工厂事件自变量记录此结构$ c>函数使用JSDoc吗?

How do I document this structure for events argument in my factory function using JSDoc?

我唯一想到的解决方案是将个事件制成一个数组,然后可以使用 typedef 像这样:

The only solution I can think of is to make events an array, then I can use typedef like this:

/**
 * @typedef event
 * @property {function} before
 * @property {function} after
 */
/**
 * @typedef eventTuple
 * @property {string} key
 * @property {event} event
 */
/**
 * @param {[eventTuple]} events
 */
function factory(events) {
  for(const { key, event } of events) {
    const { before, after } = event
  }
}

但是我真的想保留原始结构。

But I really wanna keep the original structure.

是否可以在我的原始结构中记录此事件类型定义?

Is it possible to document this event type definition in my original structure?

我主要担心它在VSCode中的工作,该代码从JSDoc中提取了这些类型定义。

I'm mainly concerned about it working in VSCode which lifts these type definitions from JSDoc.

推荐答案

您可以使用TS语法。

ps老实说,我不确定这是否到处都适用。但这至少适用于智能感知

p.s. honestly, I'm not sure if this works everywhere. But it works for intellisense at least

/**
 * @typedef event
 * @property {function} before
 * @property {function} after
 */

/**
 * @param {{[key: string]: event}} events
 */
function factory(events) {
    for (const key in events) {
        const { before, after } = events[key]
    }
}

这篇关于JSDoc中对象中任意键的值的文档结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆