javascripttypescriptvisual-studio-codeintellisensejsdoc

how jsdoc Classes with static properties


how i can get a valid setup here? enter image description here

I want able to doc the id property with the static classes _Test.list but am not able to found the correct way with intellisense inside vscode. So all number not come from _Test.list dictionary, should give me error. enter image description here

Any body can help me to format correctly this with jsdoc plz. Sorry if is a noob questions, am starting with jsdoc.

class _Test {
    static list = { a:1,b:2,c:3 };
    constructor() {
        /**
        * @typedef {Object} DATA
        * @property {_Test.list} DATA.id - id from list _Test.list
        * @property {_Test.list} DATA.id2 - id from list _Test.list
        * 
        */
        /**@type {DATA} */
        this.list = {
            id: _Test.list.a, // should ok
            id2: 14, // should show a error
        }
    }
};

I want proceed like that's because i need keep references features inside vscode. enter image description here


Solution

  • JSDoc doesn't have a concept of as const like Typescript does, at least in VS Code's typescript. But you can explicitly give a literal type:

    /** @type {{ a: 1, b: 2, c: 3 }} */
    static list = { a: 1, b: 2, c: 3 }
    

    But it's way simpler to define your allowed values first and use them in an index signature:

    /** @typedef {1 | 2 | 3} Values */
    /** @typedef {{ [s: string]: Values }} DATA */
    
    /** @type {DATA} */
    static list = { a: 1, b: 2, c: 3 }
    

    Then you can use DATA elsewhere too.

    class _Test {
        /** @type {DATA} */
        static list = { a:1,b:2,c:3 };
        constructor() {
            /** @type {DATA} */
            this.list = {
                id: _Test.list.a, // should ok
                id2: 14, // should show a error
            }
        }
    };