How to "import" a typedef from one file to another in JSDoc using Node.js?

25,092

Import the declared type in your file File2.js using the function import.

const persons = require('./File1.js');

/**
 * @typedef {import('./File1.js').MyObject1} MyObject1
 */

class File2 {
...

It works for me.

enter image description here enter image description here

Share:
25,092

Related videos on Youtube

FireController1847
Author by

FireController1847

I am a fun person who just likes to be around and code and stuff... Not much about me.

Updated on July 09, 2022

Comments

  • FireController1847
    FireController1847 almost 2 years

    Let's say I have a file named "File1.js". In this file, I export an object of objects and I give each object a typedef, like so.

    /**
     * My typedef for each object.
     * @typedef {Object} MyObject1
     * @property {String} username Your username
     * @property {String} realname Your real name.
     * @property {boolean} isUnique Are you unique as a person?
     */
    module.exports = {
      /**
       * Person One!
       * @type {MyObject1}
       */
      myperson: {
        username: 'TheDragonSlayer',
        realname: 'George',
        isUnique: true
      },
      /**
       * Person Two!
       * @type {MyObject1}
       */
      myperson2: {
        username: 'BobMagee',
        realname: 'Bob',
        isUnique: false
      }
    }
    

    Now, in a file named "File2.js", I reference this object in a constructor and set it to a new MyObject1.

    const persons = require('./File1.js');
    
    class File2 {
      constructor(options = {}) {
        /**
         * The person for this file.
         * @type {MyObject1}
         */
        this.person = options.person ? persons[options.person] : persons.myperson2;
      }
    }
    
    module.exports = File2;
    

    I use Visual Studio Code to develop, so by pressing Ctrl+Space I get IntelliSense. Within file one and while I'm making the person objects, IntelliSense tells me that username is a string, realname is a string, and isUnique is a boolean. But, when I go into file2 and reference the newly made person via this.person, when typing this.person.username it does not come up with the expected result of "Username: String".

    Is it possible to use the typedef MyObject1 in File2 in vanilla Node.js, or am I out of luck?

    Edit: With some more information, I was able to find answers with @export and @import for TypeScript, as well as a tag of sorts that I tried as well. All of which to no avail. I also tried marking File1.js as a @module, and doing module:mymodule~MyMethod, but every time I did that it'd just mark this.person as a NodeModule instead of the method itself.

    • Peter G
      Peter G about 6 years
      Did you mean @typedef instead of @typdef in File1?
    • FireController1847
      FireController1847 about 6 years
      @PeterG Yes, sorry about that!
    • Justin Emery
      Justin Emery about 6 years
      It may just be a question of how intelligent Intellisense is rather than a JSDoc thing. Using WebStorm IDE, I found this scenario works as expected but I'm often finding limits to JSDoc support - for example it's not working as expected when the @typedef is in a dependency project.
    • Coderer
      Coderer over 3 years
      There's a comment to this effect on the one answer below, but import("some-module") is supported by Typescript but is not official JSDoc.
  • FireController1847
    FireController1847 over 5 years
    Awesome! This actually fixed it! Thank you so much!
  • glen-84
    glen-84 about 4 years
    This is TypeScript-specific syntax. Vote for github.com/jsdoc/jsdoc/issues/1645.
  • Georgiy Bukharov
    Georgiy Bukharov over 2 years
    Official docs for Import types: typescriptlang.org/docs/handbook/…