Skip to content

Use jsdoc types from node_module without requiring file #1627

@cyrilchapon

Description

@cyrilchapon

Hey.

I'm writing a tool that is able to run database migrations, just like sequelize-cli. This tool dynamically requires some migration files, that end up being like this :

module.exports = {
  up: () => {},
  down: () => {}
}

The migration script requires it, and then execute the according method. The up and down methods are then passed some arguments, like a Sequelize QueryInterface for example.

I'd like to statically annotate those arguments, to get some benefits like Visual Studio Code autocompletion inside migration files.

... but as for now, the trouble is "requiring the file holding the type definition"...

The only way I found to make this working is like this :

/* eslint-disable no-unused-vars */
const SequelizeQueryInterface = require('sequelize/lib/query-interface')
const Sequelize = require('sequelize')
/* eslint-enable no-unused-vars */

module.exports = {
  /**
   * @param {SequelizeQueryInterface} sqlQueryInterface
   * @param {Sequelize} Sequelize
   */
  up: (sqlQueryInterface, Sequelize) => {
    // <-- Types hint in VS Code work here
  },

  /**
   * @param {SequelizeQueryInterface} sqlQueryInterface
   * @param {Sequelize} Sequelize
   */
  down: ({ sqlQueryInterface, Sequelize }, { mongoConnection, Mongoose }) => {
    // <-- Types hint in VS Code work here
  }
}

As you can see, I'm forced to actually require the source file in order to "import" and then use its type definition.

The question is simple :
Is there a better way (not involving require) to tell JSDoc where to find the types ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions