简体   繁体   中英

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?

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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