简体   繁体   English

如何为具有不同类型密钥的 object 编写 JSDoc?

[英]How to write a JSDoc for an object with different types of keys?

The JSDoc api says you can document objects like so: JSDoc api 说您可以像这样记录对象

{Object.<string, number>}

and document multiple type:并记录多种类型:

{(number|boolean)}

But if I try to specify an object that could have strings OR numbers as the key, it does not work.但是,如果我尝试指定一个 object 可以将字符串或数字作为键,它就不起作用。 VSCode/JSDoc just reports the type as 'any'. VSCode/JSDoc 只是将类型报告为“任何”。

VSCode does not understand: VSCode 不明白:

/**
 * Object with string or number for keys
 * @param {Object.<(string|number), any>} Container
 */

I've also tried this in @typedef , or defining the key in it's own @typedef to no effect.我也在@typedef中尝试过这个,或者在它自己的@typedef中定义密钥无效。

Because I'm using & to get an intersection of types (like {Object.<string, any> & {'foo': number}} I don't want to have to use the boolean or to say:因为我使用&来获取类型的intersection (例如{Object.<string, any> & {'foo': number}}我不想使用 boolean 或说:

/**
 * Object with string or number for keys
 * @param {(Object.<string, any>|Object.<number, any>) & {'foo': number}} Container
 */

The type documented ends up looking something like:记录的类型最终看起来像:

 type Container = ({
    [x: string]: any;
  } & {
    'foo': number;
  }) | ({
    [x: number]: any;
  } & {
    'foo': number;
  })

Which is needlessly verbose.这是不必要的冗长。

Is there way to document this with a more succinct output?有没有办法用更简洁的 output 来记录这一点?

In JavaScript, object keys are always strings (or, in the case of numbers, coerced into strings), so you might be needlessly complicating things.在 JavaScript、object 中,键始终是字符串(或者,对于数字,强制转换为字符串),因此您可能会不必要地使事情复杂化。 See the ECMAScript spec on Objects :请参阅有关 Objects 的 ECMAScript 规范

Properties are identified using key values.属性使用键值标识。 A property key value is either an ECMAScript String value or a Symbol value.属性键值是 ECMAScript 字符串值或符号值。 All String and Symbol values, including the empty String, are valid as property keys.所有字符串和符号值,包括空字符串,都可以作为属性键有效。 A property name is a property key that is a String value.属性名称是一个属性键,它是一个字符串值。

An integer index is a String-valued property key that is a canonical numeric String integer 索引是一个字符串值的属性键,它是一个规范的数字字符串

That said, this seems like the most straightforward solution:也就是说,这似乎是最直接的解决方案:

// Combined
/**
 * @param {Object.<string, any> & {foo:  number}} Container
 */

// Split/reusable
/**
 * @typedef {Object.<string, any>} GenericObject
 * @param {GenericObject & {foo: number}} Container
 */

Both of the above result in this type/documentation:以上两种情况都导致这种类型/文档:

Container: {
    [x: string]: any;
} & {
    foo: number;
}

Declaring Object.<string, any> seems a bit redundant to me since object keys are inherently string s and values are inherently any , so declaring it this way doesn't provide much value to a developer.声明Object.<string, any>对我来说似乎有点多余,因为 object 键本质上是string并且值本质上是any ,因此以这种方式声明它不会为开发人员提供太多价值。

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

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