Search code examples
visual-studio-codejsdoc

JSDOC Reference key of other type


How does one reference the key of another object within a @type or @typedef?

/**
 * @typedef {{name: {name: string}}} moo
 * @typedef {{name: moo.name}} moo2
 */

I would expect vscode to resolve moo2 as having structure {name: {name: string}}, but it does not.


Solution

  • You have to use the bracket notation:

    /**
     * @typedef {{ name: moo["name"] }} moo2
     */
    

    If you use TS flavored JSDoc and you need to reference a property of the same name, you can also use Pick:

    /**
     * @typedef {Pick<moo, "name">} moo2 -> { name: { name: string }}
     */