8

I want to import a node module with @import, but it seems visual studio code is not getting it. Or am I doing it wrong?

Missing type in visual studio code

stoefln
  • 14,498
  • 18
  • 79
  • 138
  • I believe `@import` is a function in TypeScript, not JSDocs for JS. – Graham P Heath Mar 04 '20 at 16:38
  • AFAIK jsDoc is using TypeScript internally – stoefln Mar 05 '20 at 09:56
  • 1
    JSDocs predates TypeScript by at least 5 years. Maybe 10. As I understand it, TypeScript uses Closure Compiler, which uses JSDocs, but it’s built upon what closure could do, such as adding an `import` directive. – Graham P Heath Mar 05 '20 at 13:22
  • 1
    It's actually stupid what I wrote above. I meant: AFAIK vscode is using TypeScript internally to make sense of jsDoc directives. – stoefln Mar 05 '20 at 13:40
  • The question doesn't mention TypeScript. If you are using TS, you should add the TypeScript tag to your question. FWIW: You might want to read up on it's language server https://github.com/microsoft/TypeScript/wiki/Using-the-Language-Service-API Even if it doesn't help, it is fascinating :D – Graham P Heath Mar 05 '20 at 17:11

1 Answers1

11

Personally I would suggest TypeScript over JSDoc.

Nevertheless, try something like this? (there is no @import tag in JSDoc).

// path/to/UiStore.js

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

// path/to/another.js

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

/** @type {UiStore} */
const uiStore = {
  foo: 'hello',
  bar: 'world',
};

With mobx-state-tree it works like this:

In file UiStore.js:

export const UiStoreType = UiStore.Type

and then in path/to/another.js

/**
 * @typedef Props
 * @prop { import("../stores/UiStore").UiStoreType } uiStore
 * @prop { import("../stores/DbStore").DbStoreType } dbStore
 */
stoefln
  • 14,498
  • 18
  • 79
  • 138
87hz
  • 169
  • 3
  • This works within VSCode (assuming your eslint settings are set to TypeScript), but doesn't work with the generated docs. – Nick Bilyk Jan 23 '21 at 17:23
  • _(Diverges a little from the dir structure of your code, but..)_ is there any way to avoid rewriting the `import` directive for each StoreType that one wants to import? Something like: `@typedef {import("../stores/StoresMap")} Stores` and then simply use `@prop { Stores.UiStoreType } uiStore` & `@prop { Stores.DbStoreType } dbStore`? – Kamafeather Aug 29 '22 at 16:42