0

I looked at How to document an array of objects in JSDOC however I have a slightly different problem:

{
    1232345345: 'hello',
    1454765456: 'hello'
}

my object will contain properties that are timestamps.

What would be the best way of writing this in JSDoc? I came up with the following, but is this correct and valid JSDoc?

/**
 * @typedef {{[timestamp: number]: string}} Things
 */
basickarl
  • 25,903
  • 43
  • 172
  • 270

1 Answers1

0

Based on JSdoc's tags-type documentation (look at the section on Arrays and objects (type applications and record types), you can define a type as an Object with number keys and string values:

/**
 * @type {Object.<number, string>}
 */
const x = {
  1232345345: 'hello',
  1454765456: 'hello'
}
chazsolo
  • 6,692
  • 1
  • 15
  • 38
  • Ah superb thank you! I did see this however it didn't click. Do you know if it is possible to name the "number" as you can in TypeScript? – basickarl Nov 26 '20 at 20:51
  • You can define any other type and then use it instead of number, like `{Object.}` - is that what you mean? – chazsolo Nov 26 '20 at 20:53
  • Ah no, in VSCode when using `[timestamp: number]` the intellisense shows `timestamp` when hovering over however when I used your answer it showed `x`. I hope you understand what I mean! – basickarl Nov 26 '20 at 20:57
  • Ah, understood - I'm not sure if that's possible but good question! – chazsolo Nov 29 '20 at 19:01
  • I got an answer here: https://github.com/gajus/eslint-plugin-jsdoc/issues/657 Currently JSDoc does not support so called `tuples`. – basickarl Nov 29 '20 at 19:19