Nursultan Zarlyk
Nursultan Zarlyk

Reputation: 412

VS Code - how to document an object using JSDoc

I have a variable storage, that stores all elements of type Person. So basically it is a dictionary with key URL and value PersonInstance:

let storage = {}
storage["localhost.com/person/1"] = new Person("John Wick");

Actually I could do it by writing:

/** 
 * @type {Object.<string, Person>}
 */

But VS Code Intellisense does not work in such case. Is there other way to document such object with unlimited number of keys in VS Code?

Upvotes: 3

Views: 3210

Answers (3)

Jimmy Thomsen
Jimmy Thomsen

Reputation: 561

Missing support for Object<string, T> has been added. However, be aware that casing matters!

Works

Object<string, T> where string is the index and T is the value type.

Does NOT work

object<string, T> - object MUST start with upper case: Object

Object<String, T> - String MUST be all lower case: string

Upvotes: 2

Nursultan Zarlyk
Nursultan Zarlyk

Reputation: 412

I found that it is better to use Map instead of object. So basically you can write something like this

    /**
     * @type {Map<string, Person>}
     */
    let bsStorage = new Map();

NOTE: And if you concern about older browsers, that do not support ES6, you can transpile using Babel.

Upvotes: 0

Matt Bierner
Matt Bierner

Reputation: 65185

VScode's javascript intellisense is powered by TypeScript which unfortunatly does not currently support the Object.<string, Person> type syntax. Here's the bug tracking this.

You can work around this by using typescript syntax for the map:

/**
 * @type {{ [key: string]: Person }}
 */
const storage = {}

which will improve the intellisense in vscode but is not a standard or widely supported jsdoc type.

Upvotes: 3

Related Questions