繁体   English   中英

对象内的动态键名

[英]Dynamic key name inside an object

我正在尝试执行以下操作:我有一个看起来像这样的对象

const object = {
    healthcheck: [
        { method: 'get', name: 'test' }
    ],
    users: [
        { method: 'get', name: 'auth' }
    ],
};

所以我有一个有键的对象,每个键可以是任何字符串,每个键都是一个对象数组,其中的方法和名称作为键。

如何使用 JSDoc 在对象内部使用动态键提供智能感知?

谢谢! 😉

您可以通过以下语法输入提示动态键: {[key: string]: any} ,因此在您的情况下,这应该会得到您正在寻找的结果:

/**
 * @typedef {{
 *     [key: string]: [{
 *         method: string,
 *         name: string
 *     }]
 * }} MyObject
 */

/**
 * @type {MyObject}
*/
const object = {
    healthcheck: [
        { method: 'get', name: 'test' }
    ],
    users: [
        { method: 'get', name: 'auth' }
    ],
}

typedef 是为了方便在你的代码中的各个地方使用类型,但是如果你需要智能感知来显示类型的完整形状,你可以直接使用类型:

/**
 * @param {{
 *     [key: string]: [{
 *         method: string,
 *         name: string
 *     }]
 * }} routes
 */
const xt = routes => {}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM