如何让 jsDoc “导入”与 vscode 一起工作?

sto*_*fln 8 javascript node.js jsdoc visual-studio-code mobx-state-tree

我想使用 @import 导入节点模块,但 Visual Studio 代码似乎无法获取它。还是我做错了?

Visual Studio 代码中缺少类型

小智 11

就我个人而言,我建议使用 TypeScript 而不是 JSDoc。

尽管如此,尝试这样的事情吗?(JSDoc 中没有@import标签)。

// path/to/UiStore.js

/**
 * @typedef UiStore
 * @type {object}
 * @property {string} foo - description for foo
 * @property {string} bar - description for bar
 */

Run Code Online (Sandbox Code Playgroud)
// path/to/another.js

/** @typedef {import("path/to/UiStore").UiStore} UiStore */

/** @type {UiStore} */
const uiStore = {
  foo: 'hello',
  bar: 'world',
};
Run Code Online (Sandbox Code Playgroud)

使用 mobx-state-tree 它的工作原理如下:

在文件 UiStore.js 中:

export const UiStoreType = UiStore.Type
Run Code Online (Sandbox Code Playgroud)

然后在path/to/another.js中

/**
 * @typedef Props
 * @prop { import("../stores/UiStore").UiStoreType } uiStore
 * @prop { import("../stores/DbStore").DbStoreType } dbStore
 */
Run Code Online (Sandbox Code Playgroud)