使用babel保存jsdoc注释和VS Code智能感知

时间:2019-04-19 17:07:22

标签: visual-studio-code babeljs intellisense jsdoc

我有一个用现代ECMAScript编写的用于与服务器APIS通信的javascript库。

它已完全记录了JSDoc注释:

/**
 *  @class - TODOS API Client class
 */
class todosApi {
    /**
   * Gets Todos, given the parameters
   * @param {number} personId
   * @param {number} [year]
   * @param {number} [month]
   * @param {number} [todoTypeId]
   * @returns {Object} - api response object, data will be array of todos
   */
  fetchTodos = async (....
}

在此项目的单元测试中使用此API时,在Visual Studio代码中,我从这些注释中获得了出色的智能感知,这是一件很美的事情。

但是,该库由使用create-react-app创建的单独的react应用程序使用/引用。当我通过babel运行此命令以转换为我的create-react-app应用程序可使用的格式时,它最终会像这样:

/**
 *  @class - TODOS API Client class
 */
class todosApi {
          _defineProperty(this, "fetchTodos", async (personId, eventYear, eventMonth, todoTypeId) => {
    }

我失去了对fetchTodos的理解,实际上是因为类本身如何在index.js文件中导出而失去了类本身。 babel确实可以选择默认情况下包括注释,但是该类在编译时会有点混乱,并且会丢失一些注释。

是否有任何方法可以翻译并仍保留此VS Code的智能感知?

1 个答案:

答案 0 :(得分:0)

使用tsd-jsdoc创建一个types.d.ts文件。

在package.json中添加要运行的脚本...

jsdoc -r src -t node_modules/tsd-jsdoc/dist -d lib

然后将types设置为lib/types.d.js

将该脚本作为prepublishOnly的一部分包含在内,以便在每个npm publish之前运行。