2

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

7
  • Very minor side note: You don't use a ; after a class declaration (which is what you have above). Also not after the constructor definition (nor methods). But you do use one after an assignment (in the constructor) unless you intentionally want to rely on ASI. Commented Dec 5, 2019 at 10:18
  • I put it like that for the example thinking that it would be more readable. Sorry for this. Commented Dec 5, 2019 at 10:23
  • @T.J.Crowder yes, i create a typedef where i want link DATA.id to _Test.list , I can not correctly refer to the static properties of the class, maybe I am doing badly. Commented Dec 5, 2019 at 10:26
  • _Test.list[...] are number type in this case Commented Dec 5, 2019 at 10:28
  • 1
    the issue is if i do @property {Number} DATA.id i can assign any number example 12 ,14, i want error if i assign a number not inside the static dictionary _Test.list[...] i update the message with more picture. Commented Dec 5, 2019 at 10:32

3 Answers 3

1

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
        }
    }
};
Sign up to request clarification or add additional context in comments.

1 Comment

wow awesome , thanks you so much, i waste so many time to try make work @enum with vscode, your solution work.
1

I don't know if this was possible when this was first asked. But it's definitely possible now.

const list = { a:1, b:2, c:3 };
// Will be typed as 
{
    a: number;
    b: number;
    c: number;
}

But if you actually type it as a const, then you'll get what you're asking.

const list = /** @type {const} */ ({ a:1, b:2, c:3 });
// Will be typed as
{
    readonly a: 1;
    readonly b: 2;
    readonly c: 3;
}

The important parts here are const type and also wrapping the desired value in parenthesis. Both of these are required for it to actually be seen as a const. It being static or not is irrelevant and works the same for both.

Comments

0

just want update now intellisence support jsdoc mix with ts so we can use @type {keyof staticList}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.